我正在尝试使用Play 2.3和securesocial的主要快照创建一个Java应用程序,允许用户使用Facebook,Google +或Twitter登录,允许用户将他们的帐户链接在一起并将此信息保存到数据库。我一直在编写UserService(特别是在链接帐户时)时遇到这种情况。我找到了使用早期版本的Play / securesocial的示例,但是无法使用主快照找到示例。
如果有人有任何可以分享的例子,我们将不胜感激。
答案 0 :(得分:1)
我能够创建UserService。
用户模型
import play.db.ebean.Model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity
@Table(name = "users")
public class User extends Model {
@Id
@GeneratedValue
public Long id;
public Date lastLogin;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
public List<Profile> identities;
public User(Profile profile){
identities = new ArrayList<Profile>();
this.identities.add(profile);
lastLogin = new Date();
}
public static Finder<String ,User> find = new Finder<String, User>(String.class, User.class);
}
个人资料模型
import play.db.ebean.Model;
import securesocial.core.BasicProfile;
import javax.persistence.*;
@Entity
@Table(name = "profiles")
public class Profile extends Model {
@Id
@GeneratedValue
public Long id;
public String providerId;
public String authUserId;
public String firstName;
public String lastName;
public String fullName;
public String email;
public String avatarUrl;
@ManyToOne
@JoinColumn(name = "user_id")
public User user;
public Profile(BasicProfile profile){
this.providerId = profile.providerId();
this.authUserId = profile.userId();
if(profile.firstName().isDefined())
firstName = profile.firstName().get();
if(profile.lastName().isDefined())
lastName = profile.lastName().get();
if(profile.fullName().isDefined())
fullName = profile.fullName().get();
if(profile.email().isDefined())
email = profile.email().get();
if(profile.avatarUrl().isDefined())
avatarUrl = profile.avatarUrl().get();
}
}
用户服务
package services;
import models.Profile;
import models.User;
import play.libs.F;
import securesocial.core.BasicProfile;
import securesocial.core.PasswordInfo;
import securesocial.core.java.BaseUserService;
import securesocial.core.java.Token;
import securesocial.core.services.SaveMode;
import java.util.Date;
import java.util.List;
public class DemoUserService extends BaseUserService<User> {
@Override
public F.Promise<User> doSave(BasicProfile basicProfile, SaveMode saveMode) {
User result = null;
if(saveMode == SaveMode.SignUp()){
Profile profile = new Profile(basicProfile);
result = new User(profile);
profile.user = result;
result.save();
}else if(saveMode == SaveMode.LoggedIn()){
List<User> users = User.find.all();
for(User user: users){
for(Profile p : user.identities) {
if (p.authUserId.equals(basicProfile.userId()) && p.providerId.equals(basicProfile.providerId())) {
result = user;
result.lastLogin = new Date();
result.update();
}
}
}
}else{
throw new RuntimeException("Unknown mode");
}
return F.Promise.pure(result);
}
@Override
public F.Promise<User> doLink(User user, BasicProfile basicProfile) {
User target;
User u = User.find.byId(user.id.toString());
if(u == null) {
throw new RuntimeException("Cant find user");
}
target = u;
boolean linked = false;
for(Profile p : target.identities){
if(p.authUserId.equals(basicProfile.userId()) && p.providerId.equals(basicProfile.providerId())){
linked = true;
}
}
if(!linked) {
target.identities.add(new Profile(basicProfile));
target.update();
}
return F.Promise.pure(target);
}
@Override
public F.Promise<BasicProfile> doFind(String providerId, String userId) {
BasicProfile found = null;
List<User> users = User.find.all();
for(User u: users){
for(Profile i : u.identities){
if(i.providerId.equals(providerId) && i.authUserId.equals(userId)){
found = new BasicProfile(providerId , userId , null , null , null , null , null , null , null , null , null);
break;
}
}
}
return F.Promise.pure(found);
}
@Override
public F.Promise<BasicProfile> doFindByEmailAndProvider(String s, String s1) {
return null;
}
@Override
public F.Promise<Token> doSaveToken(Token token) {
return null;
}
@Override
public F.Promise<Token> doDeleteToken(String s) {
return null;
}
@Override
public void doDeleteExpiredTokens() {
}
@Override
public F.Promise<Token> doFindToken(String s) {
return null;
}
@Override
public F.Promise<PasswordInfo> doPasswordInfoFor(User user) {
return null;
}
@Override
public F.Promise<BasicProfile> doUpdatePasswordInfo(User user, PasswordInfo passwordInfo) {
return null;
}
}