标签:
截取自项目中的一个service实现类,记录一下:
base类
1 package com.bupt.auth.service.base; 2 3 import javax.annotation.Resource; 4 5 import com.bupt.auth.dao.OauthClientDao; 6 import com.bupt.auth.dao.PermissionDao; 7 import com.bupt.auth.dao.ResourceDao; 8 import com.bupt.auth.dao.RoleDao; 9 import com.bupt.auth.dao.TokenDao; 10 import com.bupt.auth.dao.UserDao; 11 12 public class BaseManagerImpl 13 { 14 @Resource 15 protected UserDao userDao; 16 @Resource 17 protected RoleDao roleDao; 18 @Resource 19 protected ResourceDao resourceDao; 20 @Resource 21 protected PermissionDao permissionDao; 22 @Resource 23 protected TokenDao tokenDao; 24 @Resource(name="oauthDao") 25 protected OauthClientDao clientDao; 26 27 public BaseManagerImpl(){} 28 29 }
具体业务的实现类:
1 package com.bupt.auth.service.impl; 2 3 import java.util.HashSet; 4 import java.util.List; 5 import java.util.Set; 6 import java.util.SortedSet; 7 import java.util.TreeSet; 8 9 import org.springframework.stereotype.Service; 10 11 import com.bupt.auth.dao.RoleDao; 12 import com.bupt.auth.entity.Permission; 13 import com.bupt.auth.entity.Resource; 14 import com.bupt.auth.entity.Role; 15 import com.bupt.auth.entity.User; 16 import com.bupt.auth.exception.MyException; 17 import com.bupt.auth.service.RoleManager; 18 import com.bupt.auth.service.base.BaseManagerImpl; 19 @Service("roleManager") 20 public class RoleManagerImpl extends BaseManagerImpl implements RoleManager 21 { 22 23 24 25 26 @Override 27 public Role findRoleAdminByUserId(Long id) { 28 // TODO Auto-generated method stub 29 Role role = roleDao.findRoleAdminByUserId(id); 30 role = roleDao.loadRoleByRoleId(role.getId()); 31 32 return role; 33 } 34 35 @Override 36 public void update(Role role) { 37 // TODO Auto-generated method stub 38 roleDao.update(role); 39 } 40 41 @Override 42 public boolean addRole(Role role) throws MyException { 43 // TODO Auto-generated method stub 44 if(roleDao.findRoleByRoleNameAndUserId(role.getOwnerUser().getId(), role.getName()) != null){ 45 throw new MyException("This role has already exist!!", "300"); 46 } 47 try{ 48 roleDao.save(role); 49 }catch(Exception e){ 50 return false; 51 } 52 return true; 53 } 54 55 @Override 56 public List<Role> findRoleByUserId(Long id) { 57 // TODO Auto-generated method stub 58 return roleDao.findRoleByUserId(id); 59 } 60 61 @Override 62 public boolean deleteRole(Long id) { 63 // TODO Auto-generated method stub 64 try{ 65 roleDao.delete(id); 66 }catch(Exception e){ 67 return false; 68 } 69 return true; 70 } 71 72 @Override 73 public Role getById(Long id) { 74 // TODO Auto-generated method stub 75 return roleDao.loadRoleByRoleId(id); 76 } 77 78 @Override 79 public Role findRoleByRoleNameAndUserId(Long id, String rolename) { 80 // TODO Auto-generated method stub 81 return roleDao.findRoleByRoleNameAndUserId(id, rolename); 82 } 83 84 85 @Override 86 public SortedSet<Long> findRoleByVePeId(Set<String> vpId, int veorpe) 87 { 88 Set<User> user = getUserFromVPId(vpId, veorpe); 89 return getRoleFromUser(user); 90 91 } 92 93 private SortedSet<Long> getRoleFromUser(Set<User> user) 94 { 95 SortedSet<Long> result = new TreeSet<Long>(); 96 for (User u : user) 97 { 98 //该用户的所有角色 99 List<Role> role = roleDao.findRoleByUserId(u.getId()); 100 //将角色对应的ID放到result中 101 for (Role r : role)//如果角色没有设置规则,那么将id的负值放到result中 102 { 103 if (!r.getName().equals("administrator")) 104 result.add(r.getConstraintRole().equals("public") ? -r.getId() : r.getId()); 105 } 106 } 107 108 return result; 109 } 110 111 private Set<User> getUserFromVPId(Set<String> vpId, int veorpe) 112 { 113 Set<User> user = new HashSet<User>(); 114 for (String id : vpId) 115 { 116 Resource resource = resourceDao.findResourceByResId(id, veorpe); 117 user.add(resource.getUser()); 118 } 119 return user; 120 } 121 122 @Override 123 public List<Role> findRoleByPermId(Long id) { 124 // TODO Auto-generated method stub 125 return roleDao.findRoleByPermId(id); 126 } 127 128 @Override 129 public boolean deleteRolesPermissionByRoleIdAndPermId(Role role, 130 Long permid) throws MyException { 131 // TODO Auto-generated method stub 132 return roleDao.deleteRolesPermissionByRoleIdAndPermId(role, permid); 133 } 134 135 @Override 136 public boolean updateRole(Role role, Set<Long> old, 137 Set<Long> addperm) throws MyException { 138 // TODO Auto-generated method stub 139 //Role role = this.getById(roleid); 140 141 if(old != null && old.size() != 0){ 142 for(Long permid : old){ 143 this.deleteRolesPermissionByRoleIdAndPermId(role, permid); 144 } 145 } 146 147 if(addperm != null && addperm.size() != 0){ 148 for(Long permid : addperm){ 149 Permission perm = permissionDao.getById(permid); 150 Set<Permission> permset = role.getPermissions(); 151 permset.add(perm); 152 this.update(role); 153 } 154 } 155 156 return true; 157 } 158 159 @Override 160 public Role generateRole(String rolename, String description, 161 Set<Long> permissions, User user) { 162 // TODO Auto-generated method stub 163 try{ 164 Role role = new Role(); 165 166 role.setName(rolename); 167 role.setConstraintRole("public"); 168 role.setDescription(description); 169 role.setOwnerUser(user); 170 171 Set<String> accessTokens = new HashSet<String>(); 172 accessTokens.add(user.getAccesstoken()); 173 role.setAccessTokens(accessTokens); 174 175 roleDao.save(role); 176 Role adminrole = roleDao.findRoleAdminByUserId(user.getId()); 177 Set<Permission> totalPerm = adminrole.getPermissions(); 178 179 if(totalPerm == null || totalPerm.size() == 0) 180 return null; 181 182 Set<Permission> set = new HashSet<Permission>(); 183 for(Long perm_id : permissions){ 184 Permission perm = permissionDao.loadPermissionById(perm_id); 185 if(perm != null && totalPerm.contains(perm)){ 186 set.add(perm); 187 } 188 } 189 190 role.setPermissions(set); 191 roleDao.update(role); 192 return role; 193 }catch(Exception e){ 194 return null; 195 } 196 } 197 198 @Override 199 public boolean updateRole(Long id, Set<Long> permissions) throws MyException { 200 // TODO Auto-generated method stub 201 Set<Long> old = new HashSet<Long>(); 202 Role role = roleDao.loadRoleByRoleId(id); 203 204 if(role == null){ 205 throw new MyException("Role Not Found", "301"); 206 } 207 208 Set<Permission> pset = role.getPermissions(); 209 if(pset != null && pset.size() != 0){ 210 for(Permission perm:pset){ 211 old.add(perm.getId()); 212 } 213 updateRole(role, old, null); 214 } 215 216 Role adminrole = roleDao.findRoleAdminByUserId(role.getOwnerUser().getId()); 217 Set<Permission> totalPerm = adminrole.getPermissions(); 218 if(totalPerm == null || totalPerm.size() == 0){ 219 return false; 220 } 221 222 for(Long perm_id:permissions){ 223 if(permissionDao.getById(perm_id) == null){ 224 permissions.remove(perm_id); 225 } 226 } 227 228 if(permissions != null && permissions.size() != 0) 229 updateRole(role, null, permissions); 230 231 return true; 232 } 233 }
标签:
原文地址:http://www.cnblogs.com/godlei/p/5615081.html