Example #1
0
  /**
   * Saves a role.
   *
   * @param role the role to save
   * @param currentUser the user performing the save operation
   */
  public void saveRole(Role role, User currentUser) throws IOException {
    Assert.notNull(role);
    Assert.notNull(currentUser);

    ILockedRepository repo = null;
    try {
      repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false);

      Map<String, Object> roleMap = new HashMap<String, Object>();
      roleMap.put("name", role.getName()); // $NON-NLS-1$
      Set<String> permissions = Sets.newHashSet();
      for (Permission permission : role.getPermissions()) {
        permissions.add(permission.name());
      }
      roleMap.put("permissions", permissions); // $NON-NLS-1$

      Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
      String json = gson.toJson(roleMap);
      File workingDir = RepositoryUtil.getWorkingDir(repo.r());
      File workingFile = new File(workingDir, role.getName() + ROLE_SUFFIX);
      FileUtils.write(workingFile, json, Charsets.UTF_8);

      Git git = Git.wrap(repo.r());
      git.add().addFilepattern(role.getName() + ROLE_SUFFIX).call();
      PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail());
      git.commit().setAuthor(ident).setCommitter(ident).setMessage(role.getName()).call();
    } catch (GitAPIException e) {
      throw new IOException(e);
    } finally {
      Util.closeQuietly(repo);
    }
  }
 public EList<Permission> getRolepermissions(Role value) {
   if (!(value == null)) {
     EList<Permission> res = value.getPermissions();
     return res;
   }
   return null;
 }
Example #3
0
 private List<GrantedAuthority> populateGrantedAuthorities() {
   final List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
   for (final Role role : this.roles) {
     final Collection<Permission> permissions = role.getPermissions();
     for (final Permission permission : permissions) {
       grantedAuthorities.add(new SimpleGrantedAuthority(permission.getCode()));
     }
   }
   return grantedAuthorities;
 }
 /**
  * Salva su db i permessi di un ruolo.
  *
  * @param role Il ruolo
  * @param conn La connessione al db
  * @throws ApsSystemException In caso di eccezione nell'accesso al db.
  */
 private void addRolePermissions(Role role, Connection conn) throws ApsSystemException {
   Set<String> permissions = role.getPermissions();
   if (permissions != null && permissions.size() > 0) {
     PreparedStatement stat = null;
     try {
       String roleName = role.getName();
       Iterator<String> permissionIter = role.getPermissions().iterator();
       stat = conn.prepareStatement(ADD_ROLE_PERMISSION);
       while (permissionIter.hasNext()) {
         stat.setString(1, roleName);
         stat.setString(2, (String) permissionIter.next());
         stat.addBatch();
         stat.clearParameters();
       }
       stat.executeBatch();
     } catch (Throwable t) {
       processDaoException(t, "Error while adding permissions to a role", "addRolePermissions");
     } finally {
       closeDaoResources(null, stat);
     }
   }
 }
Example #5
0
 /**
  * Converts a {@link RoleGrantedAuthority} to a set of {@link PermissionGrantedAuthority}. This
  * method will return an empty set if the role specified by the RoleGrantedAuthority does not
  * exist (rather than throwing a {@link RoleNotFoundException}.)
  */
 Set<PermissionGrantedAuthority> toPermissionGrantedAuthorities(RoleGrantedAuthority rga)
     throws IOException {
   Set<PermissionGrantedAuthority> result = Sets.newHashSet();
   try {
     Role role = getRole(rga.getRoleName());
     GrantedAuthorityTarget target = rga.getTarget();
     for (Permission permission : role.getPermissions()) {
       result.add(new PermissionGrantedAuthority(target, permission));
     }
   } catch (RoleNotFoundException e) {
     // role might have been deleted
   }
   return result;
 }