diff --git a/others/database/initial_data.ddl b/others/database/initial_data.ddl index d399e46..8d1d98e 100644 --- a/others/database/initial_data.ddl +++ b/others/database/initial_data.ddl @@ -1,9 +1,26 @@ --- Initial database pre data --- admin, password is admin +-- Initial database data + +truncate user_; +truncate user_privilege; +-- admin, password is admin ( All privileges) insert into user_(id,guid,create_time,email,password,phone,username,default_user) values (21,'29f6004fb1b0466f9572b02bf2ac1be8',now(),'admin@wdcy.cc','21232f297a57a5a743894a0e4a801fc3','028-1234567','admin',true); +-- unity, password is unity ( ROLE_UNITY) +insert into user_(id,guid,create_time,email,password,phone,username,default_user) +values +(22,'55b713df1c6f423e842ad68668523c49',now(),'unity@wdcy.cc','439b3a25b555b3bc8667a09a036ae70c','','unity',false); + +insert into user_privilege(user_id,privilege) values (22,'UNITY'); + +-- mobile, password is mobile ( ROLE_MOBILE) +insert into user_(id,guid,create_time,email,password,phone,username,default_user) +values +(23,'612025cb3f964a64a48bbdf77e53c2c1',now(),'mobile@wdcy.cc','532c28d5412dd75bf975fb951c740a30','','mobile',false); + +insert into user_privilege(user_id,privilege) values (23,'MOBILE'); + -- initial oauth client details test data -- 'unity-client' support browser,mobile-device visit diff --git a/others/database/initial_db.ddl b/others/database/initial_db.ddl index cc46374..7140dfd 100644 --- a/others/database/initial_db.ddl +++ b/others/database/initial_db.ddl @@ -27,3 +27,13 @@ CREATE TABLE `user_` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8; + +-- ############### +-- Domain: Privilege +-- ############### +Drop table if exists user_privilege; +CREATE TABLE `user_privilege` ( + `user_id` int(11), + `privilege` varchar(255) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + diff --git a/src/main/java/cc/wdcy/domain/shared/security/WdcyUserDetails.java b/src/main/java/cc/wdcy/domain/shared/security/WdcyUserDetails.java index 3f22e2a..4037be8 100644 --- a/src/main/java/cc/wdcy/domain/shared/security/WdcyUserDetails.java +++ b/src/main/java/cc/wdcy/domain/shared/security/WdcyUserDetails.java @@ -1,12 +1,14 @@ package cc.wdcy.domain.shared.security; +import cc.wdcy.domain.user.Privilege; import cc.wdcy.domain.user.User; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; /** * @author Shengzhao Li @@ -14,27 +16,43 @@ import java.util.Collection; public class WdcyUserDetails implements UserDetails { protected static final String ROLE_PREFIX = "ROLE_"; - protected static final GrantedAuthority DEFAULT_USER_ROLE = new SimpleGrantedAuthority(ROLE_PREFIX + "USER"); + protected static final GrantedAuthority DEFAULT_USER_ROLE = new SimpleGrantedAuthority(ROLE_PREFIX + Privilege.USER.name()); protected User user; + protected List grantedAuthorities = new ArrayList<>(); public WdcyUserDetails() { } public WdcyUserDetails(User user) { this.user = user; + initialAuthorities(); + } + + private void initialAuthorities() { + //Default, everyone have it + this.grantedAuthorities.add(DEFAULT_USER_ROLE); + //default user have all privileges + if (user.defaultUser()) { + this.grantedAuthorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + Privilege.UNITY.name())); + this.grantedAuthorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + Privilege.MOBILE.name())); + } else { + final List privileges = user.privileges(); + for (Privilege privilege : privileges) { + this.grantedAuthorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + privilege.name())); + } + } } /** - * Note: please change the codes in this method , make it is much more available . - * Just for test of current implements. + * Return authorities, more information see {@link #initialAuthorities()} * * @return Collection of GrantedAuthority */ @Override public Collection getAuthorities() { - return Arrays.asList(DEFAULT_USER_ROLE, new SimpleGrantedAuthority(ROLE_PREFIX + "UNITY"), new SimpleGrantedAuthority(ROLE_PREFIX + "MOBILE")); + return this.grantedAuthorities; } @Override diff --git a/src/main/java/cc/wdcy/domain/user/Privilege.java b/src/main/java/cc/wdcy/domain/user/Privilege.java new file mode 100644 index 0000000..880d570 --- /dev/null +++ b/src/main/java/cc/wdcy/domain/user/Privilege.java @@ -0,0 +1,12 @@ +package cc.wdcy.domain.user; + +/** + * @author Shengzhao Li + */ +public enum Privilege { + + USER, //Default privilege + + UNITY, + MOBILE +} \ No newline at end of file diff --git a/src/main/java/cc/wdcy/domain/user/User.java b/src/main/java/cc/wdcy/domain/user/User.java index 339bfff..a280dd3 100644 --- a/src/main/java/cc/wdcy/domain/user/User.java +++ b/src/main/java/cc/wdcy/domain/user/User.java @@ -2,7 +2,9 @@ package cc.wdcy.domain.user; import cc.wdcy.domain.AbstractDomain; +import java.util.ArrayList; import java.util.Date; +import java.util.List; /** * @author Shengzhao Li @@ -20,6 +22,8 @@ public class User extends AbstractDomain { private Date lastLoginTime; + private List privileges = new ArrayList<>(); + public User() { } @@ -50,6 +54,10 @@ public class User extends AbstractDomain { return email; } + public List privileges() { + return privileges; + } + @Override public String toString() { final StringBuilder sb = new StringBuilder(); @@ -80,7 +88,6 @@ public class User extends AbstractDomain { } - public Date lastLoginTime() { return lastLoginTime; } diff --git a/src/main/resources/cc/wdcy/infrastructure/mybatis/UserMapper.xml b/src/main/resources/cc/wdcy/infrastructure/mybatis/UserMapper.xml index cd3b47a..900f7a0 100644 --- a/src/main/resources/cc/wdcy/infrastructure/mybatis/UserMapper.xml +++ b/src/main/resources/cc/wdcy/infrastructure/mybatis/UserMapper.xml @@ -17,6 +17,8 @@ + + @@ -28,6 +30,10 @@ select * from user_ where id = #{id} + +