Пример #1
0
 public void updateProjectPermission(
     Project project, String name, Permission perm, boolean group, User modifier)
     throws ProjectManagerException {
   logger.info(
       "User "
           + modifier.getUserId()
           + " updating permissions for project "
           + project.getName()
           + " for "
           + name
           + " "
           + perm.toString());
   projectLoader.updatePermission(project, name, perm, group);
   if (group) {
     projectLoader.postEvent(
         project,
         EventType.GROUP_PERMISSION,
         modifier.getUserId(),
         "Permission for group " + name + " set to " + perm.toString());
   } else {
     projectLoader.postEvent(
         project,
         EventType.USER_PERMISSION,
         modifier.getUserId(),
         "Permission for user " + name + " set to " + perm.toString());
   }
 }
Пример #2
0
  public boolean hasPermission(User user, Type type) {
    Permission perm = userPermissionMap.get(user.getUserId());
    if (perm != null && (perm.isPermissionSet(Type.ADMIN) || perm.isPermissionSet(type))) {
      return true;
    }

    return hasGroupPermission(user, type);
  }
Пример #3
0
 public List<String> getUsersWithPermission(Type type) {
   ArrayList<String> users = new ArrayList<String>();
   for (Map.Entry<String, Permission> entry : userPermissionMap.entrySet()) {
     Permission perm = entry.getValue();
     if (perm.isPermissionSet(type)) {
       users.add(entry.getKey());
     }
   }
   return users;
 }
Пример #4
0
  public List<Project> getUserProjects(User user) {
    ArrayList<Project> array = new ArrayList<Project>();
    for (Project project : projectsById.values()) {
      Permission perm = project.getUserPermission(user);

      if (perm != null && (perm.isPermissionSet(Type.ADMIN) || perm.isPermissionSet(Type.READ))) {
        array.add(project);
      }
    }
    return array;
  }
Пример #5
0
  public boolean hasGroupPermission(User user, Type type) {
    for (String group : user.getGroups()) {
      Permission perm = groupPermissionMap.get(group);
      if (perm != null) {
        if (perm.isPermissionSet(Type.ADMIN) || perm.isPermissionSet(type)) {
          return true;
        }
      }
    }

    return false;
  }
Пример #6
0
  public boolean hasUserPermission(User user, Type type) {
    Permission perm = userPermissionMap.get(user.getUserId());
    if (perm == null) {
      // Check group
      return false;
    }

    if (perm.isPermissionSet(Type.ADMIN) || perm.isPermissionSet(type)) {
      return true;
    }

    return false;
  }
Пример #7
0
  public Permission getCollectivePermission(User user) {
    Permission permissions = new Permission();
    Permission perm = userPermissionMap.get(user.getUserId());
    if (perm != null) {
      permissions.addPermissions(perm);
    }

    for (String group : user.getGroups()) {
      perm = groupPermissionMap.get(group);
      if (perm != null) {
        permissions.addPermissions(perm);
      }
    }

    return permissions;
  }
Пример #8
0
  public List<Project> getUserProjectsByRegex(User user, String regexPattern) {
    List<Project> array = new ArrayList<Project>();
    Pattern pattern;
    try {
      pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE);
    } catch (PatternSyntaxException e) {
      logger.error("Bad regex pattern " + regexPattern);
      return array;
    }

    for (Project project : projectsById.values()) {
      Permission perm = project.getUserPermission(user);

      if (perm != null && (perm.isPermissionSet(Type.ADMIN) || perm.isPermissionSet(Type.READ))) {
        if (pattern.matcher(project.getName()).find()) {
          array.add(project);
        }
      }
    }
    return array;
  }
Пример #9
0
  @Override
  public void updatePermission(Project project, String name, Permission perm, boolean isGroup)
      throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();

    if (this.allowsOnDuplicateKey()) {
      long updateTime = System.currentTimeMillis();
      final String INSERT_PROJECT_PERMISSION =
          "INSERT INTO project_permissions (project_id, modified_time, name, permissions, isGroup) values (?,?,?,?,?)"
              + "ON DUPLICATE KEY UPDATE modified_time = VALUES(modified_time), permissions = VALUES(permissions)";

      try {
        runner.update(
            INSERT_PROJECT_PERMISSION, project.getId(), updateTime, name, perm.toFlags(), isGroup);
      } catch (SQLException e) {
        logger.error(e);
        throw new ProjectManagerException(
            "Error updating project " + project.getName() + " permissions for " + name, e);
      }
    } else {
      long updateTime = System.currentTimeMillis();
      final String MERGE_PROJECT_PERMISSION =
          "MERGE INTO project_permissions (project_id, modified_time, name, permissions, isGroup) KEY (project_id, name) values (?,?,?,?,?)";

      try {
        runner.update(
            MERGE_PROJECT_PERMISSION, project.getId(), updateTime, name, perm.toFlags(), isGroup);
      } catch (SQLException e) {
        logger.error(e);
        throw new ProjectManagerException(
            "Error updating project " + project.getName() + " permissions for " + name, e);
      }
    }

    if (isGroup) {
      project.setGroupPermission(name, perm);
    } else {
      project.setUserPermission(name, perm);
    }
  }
Пример #10
0
  @SuppressWarnings("unchecked")
  public static Project projectFromObject(Object object) {
    Map<String, Object> projectObject = (Map<String, Object>) object;
    int id = (Integer) projectObject.get("id");
    String name = (String) projectObject.get("name");
    String description = (String) projectObject.get("description");
    String lastModifiedUser = (String) projectObject.get("lastModifiedUser");
    long createTimestamp = coerceToLong(projectObject.get("createTimestamp"));
    long lastModifiedTimestamp = coerceToLong(projectObject.get("lastModifiedTimestamp"));
    String source = (String) projectObject.get("source");
    Boolean active = (Boolean) projectObject.get("active");
    active = active == null ? true : active;
    int version = (Integer) projectObject.get("version");

    Project project = new Project(id, name);
    project.setVersion(version);
    project.setDescription(description);
    project.setCreateTimestamp(createTimestamp);
    project.setLastModifiedTimestamp(lastModifiedTimestamp);
    project.setLastModifiedUser(lastModifiedUser);
    project.setActive(active);

    if (source != null) {
      project.setSource(source);
    }

    List<Map<String, Object>> users = (List<Map<String, Object>>) projectObject.get("users");

    for (Map<String, Object> user : users) {
      String userid = (String) user.get("userId");
      Permission perm = new Permission();
      List<String> list = (List<String>) user.get("permissions");
      perm.addPermissionsByName(list);

      project.setUserPermission(userid, perm);
    }

    return project;
  }