social-login: Provide option which sync with local user name
@993f586adda6b5726c5ffd95cae3f16624ce018e
--- app/service/YonaUserServicePlugin.java
+++ app/service/YonaUserServicePlugin.java
... | ... | @@ -3,10 +3,13 @@ |
3 | 3 |
import com.feth.play.module.pa.service.UserServicePlugin; |
4 | 4 |
import com.feth.play.module.pa.user.AuthUser; |
5 | 5 |
import com.feth.play.module.pa.user.AuthUserIdentity; |
6 |
+import com.feth.play.module.pa.user.BasicIdentity; |
|
7 |
+import models.User; |
|
6 | 8 |
import models.UserCredential; |
7 | 9 |
import play.Application; |
8 | 10 |
|
9 | 11 |
public class YonaUserServicePlugin extends UserServicePlugin { |
12 |
+ private static boolean useSocialNameSync = play.Configuration.root().getBoolean("application.use.social.login.name.sync", false); |
|
10 | 13 |
|
11 | 14 |
public YonaUserServicePlugin(final Application app) { |
12 | 15 |
super(app); |
... | ... | @@ -29,12 +32,29 @@ |
29 | 32 |
// ...and dont forget to sync the cache when users get deactivated/deleted |
30 | 33 |
final UserCredential u = UserCredential.findByAuthUserIdentity(identity); |
31 | 34 |
if(u != null) { |
35 |
+ if(useSocialNameSync && identity instanceof BasicIdentity){ |
|
36 |
+ BasicIdentity authUser = ((BasicIdentity) identity); |
|
37 |
+ if(!u.name.equals(authUser.getName())){ |
|
38 |
+ updateLocalUserName(u, authUser); |
|
39 |
+ } |
|
40 |
+ } |
|
32 | 41 |
return u.id; |
33 | 42 |
} else { |
34 | 43 |
return null; |
35 | 44 |
} |
36 | 45 |
} |
37 | 46 |
|
47 |
+ private void updateLocalUserName(UserCredential u, BasicIdentity authUser) { |
|
48 |
+ u.name = authUser.getName(); |
|
49 |
+ u.update(); |
|
50 |
+ |
|
51 |
+ User localUser = User.findByEmail(authUser.getEmail()); |
|
52 |
+ if(localUser != null){ |
|
53 |
+ localUser.name = authUser.getName(); |
|
54 |
+ localUser.update(); |
|
55 |
+ } |
|
56 |
+ } |
|
57 |
+ |
|
38 | 58 |
@Override |
39 | 59 |
public AuthUser merge(final AuthUser newUser, final AuthUser oldUser) { |
40 | 60 |
if (!oldUser.equals(newUser)) { |
--- conf/application.conf.default
+++ conf/application.conf.default
... | ... | @@ -275,6 +275,9 @@ |
275 | 275 |
# Prevent using Yona's own login system |
276 | 276 |
application.use.social.login.only = false |
277 | 277 |
|
278 |
+# If true, update local user name with social login account name |
|
279 |
+application.use.social.login.name.sync = false |
|
280 |
+ |
|
278 | 281 |
# Allowed OAuth social login provider |
279 | 282 |
# choice: github, google |
280 | 283 |
application.social.login.support = "github, google" |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?