All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.web.security.actions.RolePermissionsAction Maven / Gradle / Ivy

There is a newer version: 1.20
Show newest version
/*
 * #%L
 * Nuiton Web :: Nuiton Security
 * %%
 * Copyright (C) 2012 CodeLutin, Chatellier Eric
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */

package org.nuiton.web.security.actions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.nuiton.web.SecurityTopiaPersistenceContext;
import org.nuiton.web.security.SecurityRole;
import org.nuiton.web.security.SecurityRoleTopiaDao;
import org.nuiton.web.security.SecurityUserTopiaDao;

public class RolePermissionsAction extends AbstractAction {

    /** serialVersionUID. */
    private static final long serialVersionUID = 1L;

    protected SecurityUserTopiaDao securityUserDAO;

    protected SecurityRoleTopiaDao securityRoleDAO;

    /** Id categories with name. */
    protected Map categories;
    /** Id permission with name. */
    protected Map permissions;
    /** Id categories with permissions ids. */
    protected Map> categoryPermissions;

    protected List roles;

    protected List roleIds;

    @Override
    public String input() throws Exception {
        SecurityTopiaPersistenceContext transaction = rootContext.newPersistenceContext();
        securityRoleDAO = transaction.getSecurityRoleDao();
        roles = securityRoleDAO.newQueryBuilder().setOrderByArguments(SecurityRole.PROPERTY_NAME).findAll();
        transaction.close();
        
        categories = new HashMap();
        permissions = new HashMap();
        categoryPermissions = new HashMap>();

        Properties props = config.getFlatOptions();
        for (String prop : props.stringPropertyNames()) {
            if (prop.startsWith("topia.security.permission.")) {
                String endProp = StringUtils.removeStart(prop, "topia.security.permission.");
                String[] subs = endProp.split("\\.");
                if (subs.length == 1) {
                    categories.put(subs[0], props.getProperty(prop));
                } else if (subs.length == 3) {
                    if (subs[2].equals("name")) {
                        // name
                        String name = props.getProperty(prop);
                        permissions.put(subs[1], name);
                        // association
                        Collection categoryPermissionCol = categoryPermissions.get(subs[0]);
                        if (categoryPermissionCol == null) {
                            categoryPermissionCol = new ArrayList();
                            categoryPermissions.put(subs[0], categoryPermissionCol);
                        }
                        categoryPermissionCol.add(subs[1]);
                    }
                }
            }
        }
        return super.input();
    }

    public List getRoles() {
        return roles;
    }

    public Map getCategories() {
        return categories;
    }

    public Map getPermissions() {
        return permissions;
    }

    public Map> getCategoryPermissions() {
        return categoryPermissions;
    }

    public void setRoleIds(List roleIds) {
        this.roleIds = roleIds;
    }

    @Override
    public String execute() throws Exception {
        String result = super.execute();

        if (roleIds == null) {
            result = input();
        } else {
            try {
                SecurityTopiaPersistenceContext transaction = rootContext.newPersistenceContext();
                securityRoleDAO = transaction.getSecurityRoleDao();
                for (String roleId : roleIds) {
                    SecurityRole securityRole = securityRoleDAO.findByTopiaId(roleId);
                    securityRole.clearPermissions();

                    String[] permissions = getParameters("permissions-" + roleId);
                    if (permissions != null) {
                        for (String permission : permissions) {
                            securityRole.addPermissions(permission);
                        }
                    }
                }
                transaction.commit();
            } catch (Exception ex) {
                addActionError(ex.getMessage());
                result = input();
            }
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy