Search in sources :

Example 1 with RoleValue

use of net.jforum.security.RoleValue in project jforum2 by rafaelsteil.

the class SecurityCommon method executeAddRole.

/**
	 * Execute the <i>add role</i> thing. As the SQL statement to insert user and group roles are
	 * diferent, they cannot be manipuled with a 'generic' statement, and is for this reason that
	 * <code>addRole</code> method is marked abstract. <br>
	 * The only job the <code>addRole</code> method should do is to get the correct SQL statement
	 * for each case - user or group - and the repass it to this method, who then do the job for us.
	 * 
	 * @param sql The SQL statement to be executed.
	 * @param id The ID do insert. May be user's or group's id, depending of the situation ( the caller )
	 * @param role The role name to insert
	 * @param roleValues A <code>RoleValueCollection</code> collection containing the role values to
	 * insert. If none is wanted, just pass null as argument.
	 * @param supportAutoGeneratedKeys Set to <code>true</code> if <i>Statement.RETURN_GENERATED_KEYS</i> is supported
	 * by the Driver, or <code>false</code> if not.
	 * @param autoKeysQuery String
	 */
public static void executeAddRole(String sql, int id, Role role, RoleValueCollection roleValues, boolean supportAutoGeneratedKeys, String autoKeysQuery) {
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        if (supportAutoGeneratedKeys) {
            p = JForumExecutionContext.getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        } else {
            p = JForumExecutionContext.getConnection().prepareStatement(sql);
        }
        p.setInt(1, id);
        p.setString(2, role.getName());
        p.executeUpdate();
        if (roleValues != null) {
            int roleId = -1;
            if (supportAutoGeneratedKeys) {
                rs = p.getGeneratedKeys();
                rs.next();
                roleId = rs.getInt(1);
            } else {
                p = JForumExecutionContext.getConnection().prepareStatement(autoKeysQuery);
                rs = p.executeQuery();
                if (rs.next()) {
                    roleId = rs.getInt(1);
                }
            }
            rs.close();
            rs = null;
            p.close();
            p = null;
            if (roleId == -1) {
                throw new SQLException("Could not obtain the latest role id");
            }
            p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PermissionControl.addRoleValues"));
            for (Iterator iter = roleValues.iterator(); iter.hasNext(); ) {
                RoleValue rv = (RoleValue) iter.next();
                p.setInt(1, roleId);
                p.setString(2, rv.getValue());
                p.executeUpdate();
            }
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Iterator(java.util.Iterator) RoleValue(net.jforum.security.RoleValue) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 2 with RoleValue

use of net.jforum.security.RoleValue in project jforum2 by rafaelsteil.

the class PermissionProcessHelper method addRoleValues.

private void addRoleValues(RoleValueCollection roleValues, Object[] allValues) {
    for (int i = 0; i < allValues.length; i++) {
        String value = (String) allValues[i];
        if (value == null || value.equals("")) {
            continue;
        }
        roleValues.add(new RoleValue((String) allValues[i]));
    }
}
Also used : RoleValue(net.jforum.security.RoleValue)

Example 3 with RoleValue

use of net.jforum.security.RoleValue in project jforum2 by rafaelsteil.

the class GenericGroupSecurityDAO method addRoleValue.

/**
	 * @see net.jforum.dao.GroupSecurityDAO#addRoleValue(int, net.jforum.security.Role, net.jforum.security.RoleValueCollection)
	 */
public void addRoleValue(int groupId, Role role, RoleValueCollection rvc) {
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PermissionControl.getRoleIdByName"));
        p.setString(1, role.getName());
        p.setInt(2, groupId);
        int roleId = -1;
        rs = p.executeQuery();
        if (rs.next()) {
            roleId = rs.getInt("role_id");
        }
        rs.close();
        rs = null;
        p.close();
        p = null;
        if (roleId == -1) {
            this.addRole(groupId, role, rvc);
        } else {
            p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PermissionControl.addRoleValues"));
            p.setInt(1, roleId);
            for (Iterator iter = rvc.iterator(); iter.hasNext(); ) {
                RoleValue rv = (RoleValue) iter.next();
                p.setString(2, rv.getValue());
                p.executeUpdate();
            }
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Iterator(java.util.Iterator) RoleValue(net.jforum.security.RoleValue) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 4 with RoleValue

use of net.jforum.security.RoleValue in project jforum2 by rafaelsteil.

the class SecurityCommon method loadRoles.

/**
	 * See {@link PermissionControl#executeAddRole(String, int, String, RoleValueCollection)} for
	 * explanation about this method. The working way is the same.
	 * 
	 * @param rs The ResultSet containing the data to be fetched. This method does not
	 * free the resultset after it finished using it, so it's responsability of the 
	 * caller to do such task.
	 * @return A <code>RoleCollection</code> collection with the roles processed.
	 */
public static RoleCollection loadRoles(ResultSet rs) {
    RoleCollection rc = new RoleCollection();
    try {
        Role r = null;
        String lastName = null;
        while (rs.next()) {
            String currentName = rs.getString("name");
            if (!currentName.equals(lastName)) {
                if (r != null) {
                    rc.add(r);
                }
                r = new Role();
                r.setName(rs.getString("name"));
                lastName = currentName;
            }
            String roleValue = rs.getString("role_value");
            if (!rs.wasNull() && StringUtils.isNotBlank(roleValue)) {
                r.getValues().add(new RoleValue(roleValue));
            }
        }
        if (r != null) {
            rc.add(r);
        }
        return rc;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : Role(net.jforum.security.Role) SQLException(java.sql.SQLException) RoleValue(net.jforum.security.RoleValue) RoleCollection(net.jforum.security.RoleCollection) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 5 with RoleValue

use of net.jforum.security.RoleValue in project jforum2 by rafaelsteil.

the class CategoryAction method insertSave.

// A new one
public void insertSave() {
    Category c = new Category();
    c.setName(this.request.getParameter("category_name"));
    c.setModerated("1".equals(this.request.getParameter("moderated")));
    int categoryId = this.cm.addNew(c);
    c.setId(categoryId);
    ForumRepository.addCategory(c);
    String[] groups = this.request.getParameterValues("groups");
    if (groups != null) {
        GroupSecurityDAO gmodel = DataAccessDriver.getInstance().newGroupSecurityDAO();
        PermissionControl pc = new PermissionControl();
        pc.setSecurityModel(gmodel);
        Role role = new Role();
        role.setName(SecurityConstants.PERM_CATEGORY);
        for (int i = 0; i < groups.length; i++) {
            int groupId = Integer.parseInt(groups[i]);
            RoleValueCollection roleValues = new RoleValueCollection();
            RoleValue rv = new RoleValue();
            rv.setValue(Integer.toString(categoryId));
            roleValues.add(rv);
            pc.addRoleValue(groupId, role, roleValues);
        }
        SecurityRepository.clean();
        RolesRepository.clear();
    }
    this.list();
}
Also used : Role(net.jforum.security.Role) Category(net.jforum.entities.Category) PermissionControl(net.jforum.security.PermissionControl) RoleValue(net.jforum.security.RoleValue) RoleValueCollection(net.jforum.security.RoleValueCollection) GroupSecurityDAO(net.jforum.dao.GroupSecurityDAO)

Aggregations

RoleValue (net.jforum.security.RoleValue)6 SQLException (java.sql.SQLException)3 DatabaseException (net.jforum.exceptions.DatabaseException)3 Role (net.jforum.security.Role)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 Iterator (java.util.Iterator)2 RoleValueCollection (net.jforum.security.RoleValueCollection)2 GroupSecurityDAO (net.jforum.dao.GroupSecurityDAO)1 Category (net.jforum.entities.Category)1 PermissionControl (net.jforum.security.PermissionControl)1 RoleCollection (net.jforum.security.RoleCollection)1