use of com.ganster.cms.core.pojo.User in project Ganster-CMS by Gangster-trio.
the class UserShiroRealm method doGetAuthorizationInfo.
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
User user = (User) principals.getPrimaryPrincipal();
List<Group> groupList = groupService.selectByUserId(user.getUserId());
Set<String> groupSet = groupList.stream().map(Group::getGroupName).collect(Collectors.toSet());
Set<String> permissionSet = groupSet.stream().flatMap(group -> permissionService.selectByGroupName(group).stream().map(Permission::getPermissionName)).collect(Collectors.toSet());
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setStringPermissions(permissionSet);
simpleAuthorizationInfo.setRoles(groupSet);
return simpleAuthorizationInfo;
}
use of com.ganster.cms.core.pojo.User in project Ganster-CMS by Gangster-trio.
the class PermissionServiceImplTest method interTest.
@Test
public void interTest() {
final String userName = "@#$%^&";
final String siteName = "$%^&&";
User user = new User();
user.setUserName(userName);
userService.insert(user);
Site site = new Site();
site.setSiteName(siteName);
siteService.insert(site);
Group group = new Group();
group.setGroupName(userName);
groupService.insert(group);
System.out.println(group.getGroupId());
try {
groupService.addUserToGroup(user.getUserId(), group.getGroupId());
} catch (UserNotFoundException | GroupNotFountException e) {
e.printStackTrace();
}
try {
permissionService.addUserToSite(user.getUserId(), site.getSiteId());
} catch (UserNotFoundException e) {
e.printStackTrace();
}
List<Site> siteList = permissionService.findAllUserSite(user.getUserId());
System.out.println(siteList);
try {
permissionService.addCategoryPermissionToUser(user.getUserId(), site.getSiteId(), 3, CmsConst.PERMISSION_READ);
} catch (UserNotFoundException e) {
e.printStackTrace();
}
boolean hasP = permissionService.hasCategoryPermission(user.getUserId(), site.getSiteId(), 3, CmsConst.PERMISSION_READ);
Assert.assertTrue(hasP);
}
use of com.ganster.cms.core.pojo.User in project Ganster-CMS by Gangster-trio.
the class UserShiroRealm method doGetAuthorizationInfo.
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
logger.info("进入权限配置");
String username = (String) principals.getPrimaryPrincipal();
UserExample userExample = new UserExample();
userExample.createCriteria().andUserNameEqualTo(username);
List<User> users = userService.selectByExample(userExample);
Integer j = 0;
for (User i : users) {
userId = i.getUserId();
j++;
}
if (j >= 2) {
return null;
}
User user = userService.selectByPrimaryKey(userId);
List<Group> groupList = groupService.selectByUserId(user.getUserId());
Set<String> groupSet = new HashSet<>();
for (Group i : groupList) {
if (!StringUtil.isNullOrEmpty(user.getUserName())) {
groupSet.add(i.getGroupName());
}
}
Set<String> permissionSet = new HashSet<>();
for (Group i : groupList) {
if (!StringUtil.isNullOrEmpty(i.getGroupName())) {
try {
List<Permission> permissions = permissionService.selectByGroupId(i.getGroupId());
for (Permission permission : permissions) {
permissionSet.add(permission.getPermissionName());
}
} catch (GroupNotFountException e) {
logger.info("角色未找到");
}
}
}
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setStringPermissions(permissionSet);
simpleAuthorizationInfo.setRoles(groupSet);
return simpleAuthorizationInfo;
}
use of com.ganster.cms.core.pojo.User in project Ganster-CMS by Gangster-trio.
the class UserShiroRealm method doGetAuthenticationInfo.
/**
* 认证信息.(身份验证)
* :
* Authentication 是用来验证用户身份
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String) token.getPrincipal();
String password = new String((char[]) token.getCredentials());
logger.info("-----------------------------" + password + "--------------------------------------");
UserExample userExample = new UserExample();
userExample.createCriteria().andUserNameEqualTo(username);
List<User> users = userService.selectByExample(userExample);
Integer j = 0;
for (User i : users) {
userId = i.getUserId();
j++;
}
if (j >= 2) {
throw new AuthenticationException();
}
User user = userService.selectByPrimaryKey(userId);
if (!user.getUserName().equals(username)) {
SecurityUtils.getSubject().logout();
throw new AuthenticationException();
}
if (user == null) {
throw new AuthenticationException();
}
SecurityUtils.getSubject().getSession().setAttribute("id", user.getUserId());
logger.info("用户" + user.getUserName() + "进行认证");
if (!Objects.equals(password, user.getUserPassword())) {
throw new IncorrectCredentialsException();
}
return new SimpleAuthenticationInfo(username, password, getName());
}
use of com.ganster.cms.core.pojo.User in project Ganster-CMS by Gangster-trio.
the class SiteController method add.
@PostMapping("/add")
public Message add(@RequestBody Site site) {
Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute("id");
User user = userService.selectByPrimaryKey(userId);
if (site == null) {
return super.buildMessage(1, "no data", null);
}
site.setSiteCreateTime(new Date());
site.setSiteStatus(0);
int count = siteService.insert(site);
if (count == 0) {
return super.buildMessage(1, "add site failed", null);
}
try {
if (!user.getUserName().equals("admin")) {
permissionService.addUserToSite(1, site.getSiteId());
}
permissionService.addUserToSite(userId, site.getSiteId());
} catch (UserNotFoundException e) {
e.printStackTrace();
return super.buildMessage(1, "用户未找到", null);
}
PermissionUtil.flush(userId);
return super.buildMessage(0, "success", count);
}
Aggregations