Skip to content

Commit

Permalink
social-login: Provide option which sync with local user name
Browse files Browse the repository at this point in the history
  • Loading branch information
doortts committed Feb 15, 2017
1 parent be64bfb commit 993f586
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app/service/YonaUserServicePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import com.feth.play.module.pa.service.UserServicePlugin;
import com.feth.play.module.pa.user.AuthUser;
import com.feth.play.module.pa.user.AuthUserIdentity;
import com.feth.play.module.pa.user.BasicIdentity;
import models.User;
import models.UserCredential;
import play.Application;

public class YonaUserServicePlugin extends UserServicePlugin {
private static boolean useSocialNameSync = play.Configuration.root().getBoolean("application.use.social.login.name.sync", false);

public YonaUserServicePlugin(final Application app) {
super(app);
Expand All @@ -29,12 +32,29 @@ public Object getLocalIdentity(final AuthUserIdentity identity) {
// ...and dont forget to sync the cache when users get deactivated/deleted
final UserCredential u = UserCredential.findByAuthUserIdentity(identity);
if(u != null) {
if(useSocialNameSync && identity instanceof BasicIdentity){
BasicIdentity authUser = ((BasicIdentity) identity);
if(!u.name.equals(authUser.getName())){
updateLocalUserName(u, authUser);
}
}
return u.id;
} else {
return null;
}
}

private void updateLocalUserName(UserCredential u, BasicIdentity authUser) {
u.name = authUser.getName();
u.update();

User localUser = User.findByEmail(authUser.getEmail());
if(localUser != null){
localUser.name = authUser.getName();
localUser.update();
}
}

@Override
public AuthUser merge(final AuthUser newUser, final AuthUser oldUser) {
if (!oldUser.equals(newUser)) {
Expand Down
3 changes: 3 additions & 0 deletions conf/application.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ application.maxFileSize = 2147483454
# Prevent using Yona's own login system
application.use.social.login.only = false

# If true, update local user name with social login account name
application.use.social.login.name.sync = false

# Allowed OAuth social login provider
# choice: github, google
application.social.login.support = "github, google"
Expand Down

0 comments on commit 993f586

Please sign in to comment.