00006 User add Privilege domain
parent
4a4301f02c
commit
13db3592a7
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<GrantedAuthority> 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<Privilege> 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<GrantedAuthority> getAuthorities() {
|
||||
return Arrays.asList(DEFAULT_USER_ROLE, new SimpleGrantedAuthority(ROLE_PREFIX + "UNITY"), new SimpleGrantedAuthority(ROLE_PREFIX + "MOBILE"));
|
||||
return this.grantedAuthorities;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package cc.wdcy.domain.user;
|
||||
|
||||
/**
|
||||
* @author Shengzhao Li
|
||||
*/
|
||||
public enum Privilege {
|
||||
|
||||
USER, //Default privilege
|
||||
|
||||
UNITY,
|
||||
MOBILE
|
||||
}
|
|
@ -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<Privilege> privileges = new ArrayList<>();
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
|
@ -50,6 +54,10 @@ public class User extends AbstractDomain {
|
|||
return email;
|
||||
}
|
||||
|
||||
public List<Privilege> 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;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
<result property="username" column="username"/>
|
||||
<result property="lastLoginTime" column="last_login_time"/>
|
||||
|
||||
<collection property="privileges" column="id" select="findPrivilegesByUserId" ofType="Privilege"/>
|
||||
|
||||
</resultMap>
|
||||
|
||||
|
||||
|
@ -28,6 +30,10 @@
|
|||
select * from user_ where id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="findPrivilegesByUserId" parameterType="Integer" resultType="Privilege">
|
||||
select privilege from user_privilege where user_id = #{userId}
|
||||
</select>
|
||||
|
||||
<select id="findByUsername" parameterType="String" resultMap="userMap">
|
||||
select * from user_
|
||||
where username = #{username}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
</settings>
|
||||
<typeAliases>
|
||||
<typeAlias alias="User" type="cc.wdcy.domain.user.User"/>
|
||||
<typeAlias alias="Privilege" type="cc.wdcy.domain.user.Privilege"/>
|
||||
<typeAlias alias="OauthClientDetails" type="cc.wdcy.domain.oauth.OauthClientDetails"/>
|
||||
|
||||
</typeAliases>
|
||||
|
|
|
@ -63,4 +63,20 @@ public class UserRepositoryMyBatisTest extends AbstractRepositoryTest {
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
* Run the test must initial db firstly
|
||||
* */
|
||||
@Test(enabled = false)
|
||||
public void testPrivilege() {
|
||||
|
||||
String guid = "55b713df1c6f423e842ad68668523c49";
|
||||
final User user = userRepository.findByGuid(guid);
|
||||
|
||||
assertNotNull(user);
|
||||
assertEquals(user.privileges().size(), 1);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -3,6 +3,6 @@ jdbc.driverClassName=com.mysql.jdbc.Driver
|
|||
############
|
||||
# localhost
|
||||
############
|
||||
jdbc.url=jdbc:mysql://localhost:3306/wdcy_test?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8
|
||||
jdbc.url=jdbc:mysql://localhost:3306/oauth2_test?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8
|
||||
jdbc.username=wdcy
|
||||
jdbc.password=wdcy
|
Loading…
Reference in New Issue