public static Map search(int page, int pageSize, String action, String desc) { Map map = new HashMap(); map.put("total", Permission.countByCondition(action, desc)); map.put("permissions", Permission.findByCondition(page, pageSize, action, desc)); return map; }
public static PermVO createByVO(PermVO vo) { if (vo == null) throw new RuntimeException("Permission info required"); vo.validate(); Permission p = new Permission(vo.action, vo.desc); Permission db_p = Permission.findByAction(p.action); if (db_p != null) throw new RuntimeException("Action duplicate!"); p.create(); vo.id = String.valueOf(p.id); return vo; }
public static void deleteById(Long id) { if (id == null) throw new RuntimeException("id required"); Permission permission = Permission.findById(id); if (permission == null) throw new RuntimeException("Permission not found"); try { permission.delete(); } catch (Throwable e) { throw new RuntimeException( "Could Not Delete This Permission Cause It is Assigned to Roles !"); } }
public static List<Permission> findByCondition( int page, int pageSize, String action, String desc) { final List<Object> params = new ArrayList<Object>(); final StringBuilder sb = new StringBuilder(); parseCondition(action, desc, params, sb); List<Permission> permissions = null; if (page > 0 && pageSize > 0) permissions = Permission.find(sb.toString() + " order by id desc", params.toArray()) .fetch(page, pageSize); else permissions = Permission.find(sb.toString() + " order by id desc", params.toArray()).fetch(); return permissions; }
public static void deleteByVO(PermVO vo) { if (vo == null) throw new RuntimeException("Permission info required"); if (vo.id == null) throw new RuntimeException("id required"); Permission.deleteById(Long.parseLong(vo.id)); }
public static List<TreeView> assemTreeView() { List<TreeView> result = new ArrayList<TreeView>(); List<Permission> perms = Permission.findAll(); if (perms == null) return result; TreeView root = new TreeView("0", "All Permissions", Permission.iconUrl); final Map<String, TreeView> modules = new HashMap<String, TreeView>(); for (Permission p : perms) { if (!p.action.contains(".")) continue; String[] names = p.action.split("\\."); final String moduleName = names[0]; final String name = names[1]; TreeView module = modules.get(moduleName); if (module == null) { module = new TreeView("", moduleName, Permission.iconUrl); modules.put(moduleName, module); } TreeView tv = new TreeView(String.valueOf(p.id), name, Permission.iconUrl); tv.items = null; module.expanded = true; module.items.add(tv); } root.expanded = true; root.items.addAll(modules.values()); result.add(root); return result; }
public static long countByCondition(String action, String desc) { final List<Object> params = new ArrayList<Object>(); final StringBuilder sb = new StringBuilder(); parseCondition(action, desc, params, sb); return Permission.count(sb.toString(), params.toArray()); }
public static Permission fetchById(Long id) { if (id == null) throw new RuntimeException("id required"); Permission perm = Permission.findById(id); if (perm == null) throw new RuntimeException("Permission not found"); return perm; }
/** * This method checks whether user has a permission by its id. * * @param permissionName * @return true if exists */ public boolean hasPermission(Long permissionId) { if (permissionId != null) { Permission permission = Permission.findById(permissionId); if (this.permissions.contains(permission)) { return true; } } return false; }
/** * This method checks if this Role has a permission passed as string parameter. * * @param permissionName * @return true if exists */ public boolean hasPermission(String permissionName) { if (StringUtils.isNotEmpty(permissionName)) { Permission permission = Permission.findByName(permissionName); if (this.permissions.contains(permission)) { return true; } } return false; }
public static boolean updateByJson(String models) { List<PermVO> vos = JSON.parseArray(models, PermVO.class); if (vos == null) return false; for (PermVO vo : vos) { Permission.updateByVO(vo); } return true; }
public static String createByJson(String models) { List<PermVO> vos = JSON.parseArray(models, PermVO.class); if (vos == null) return models; for (PermVO vo : vos) { Permission.createByVO(vo); } final String _models = CommonUtil.toJson(vos); return _models; }
public static List<String> assemActions() { List<String> actionSet = Permission.actions; List<Permission> perms = Permission.findAll(); List<String> actions = new ArrayList<String>(); if (actionSet != null) actionLoop: for (String a : actionSet) { for (Permission p : perms) { if (p.action.equals(a)) continue actionLoop; } actions.add(a); } return actions; }
/** * This method returns permissions that are not assigned to this role. * * @return list of Permission objects */ public static List<Permission> getNotAssignedPermissions(List<Permission> assignedPermissions) { List<Permission> allPermissionList = Permission.findAll(); // Logger.debug("Permissions count: " + allPermissionList.size()); List<Permission> res = new ArrayList<Permission>(); if (assignedPermissions != null && assignedPermissions.size() > 0) { Iterator<Permission> itrAllPermissions = allPermissionList.iterator(); while (itrAllPermissions.hasNext()) { Permission curPermission = itrAllPermissions.next(); // Logger.debug("curPermission: " + curPermission.name); if (!assignedPermissions.contains(curPermission)) { res.add(curPermission); } } } return res; }
public static void updateByVO(PermVO vo) { vo.validate(); Permission p = Permission.findById(Long.parseLong(vo.id)); if (p == null) throw new RuntimeException("Permission not found"); p.action = vo.action; p.desc = vo.desc; Permission db_p = Permission.findByAction(p.action); if (db_p != null && db_p.id != p.id) throw new RuntimeException("Action duplicate!"); p.save(); }
private static Permission findByAction(String action) { if (CommonUtil.isBlank(action)) return null; return Permission.find("byAction", action.trim()).first(); }