Ejemplo n.º 1
0
  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;
  }
Ejemplo n.º 2
0
  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;
  }
Ejemplo n.º 3
0
  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 !");
    }
  }
Ejemplo n.º 4
0
  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;
  }
Ejemplo n.º 5
0
  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));
  }
Ejemplo n.º 6
0
  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;
  }
Ejemplo n.º 7
0
  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());
  }
Ejemplo n.º 8
0
  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;
  }
Ejemplo n.º 9
0
Archivo: Role.java Proyecto: ukwa/w3act
 /**
  * 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;
 }
Ejemplo n.º 10
0
Archivo: Role.java Proyecto: ukwa/w3act
 /**
  * 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;
 }
Ejemplo n.º 11
0
  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;
  }
Ejemplo n.º 12
0
  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;
  }
Ejemplo n.º 13
0
  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;
  }
Ejemplo n.º 14
0
Archivo: Role.java Proyecto: ukwa/w3act
 /**
  * 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;
 }
Ejemplo n.º 15
0
  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();
  }
Ejemplo n.º 16
0
  private static Permission findByAction(String action) {
    if (CommonUtil.isBlank(action)) return null;

    return Permission.find("byAction", action.trim()).first();
  }