Subversion Repositories bacoAlunos

Rev

Rev 114 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.utils;

import pt.estgp.estgweb.domain.User;
import pt.estgp.estgweb.domain.GenericUser;
import pt.estgp.estgweb.utils.ConfigProperties;

import java.util.List;
import java.util.ArrayList;

import jomm.utils.MessageResources;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Jorge Machado
 * @date 28/Fev/2008
 * @time 10:56:12
 * @see pt.estgp.estgweb.utils
 */

public class RoleManager
{

    private static 1.5.0/docs/api/java/lang/String.html">String ROLE_MESSAGE_RESOURCES_PREFIX = "role.";
    public static 1.5.0/docs/api/java/lang/String.html">String ROLE_SEPARATOR = ",";

    private static List<String> roles = null;

    public static boolean hasRole(GenericUser u, 5+0%2Fdocs%2Fapi+List">List requiredRoles)
    {
        if(u == null)
            return false;
        for(5+0%2Fdocs%2Fapi+Object">Object role: requiredRoles)
        {
            if(hasRole(u,(1.5.0/docs/api/java/lang/String.html">String) role))
                return true;
        }
        return false;
    }
    public static boolean hasRole(GenericUser u, 1.5.0/docs/api/java/lang/String.html">String requiredRole)
    {
        if(u == null)
            return false;
        if(u.getRoles() == null || u.getRoles().length() == 0)
            return false;

        1.5.0/docs/api/java/lang/String.html">String[] roles = u.getRoles().split(ROLE_SEPARATOR);
        for(1.5.0/docs/api/java/lang/String.html">String role: roles)
        {
            if(role.equals(requiredRole))
                return true;
        }
        return false;
    }

    public static 5+0%2Fdocs%2Fapi+List">List readRoles()
    {
        if(roles == null)
        {
            roles = ConfigProperties.getListValues("role.");
        }
        return roles;
    }

    public static 1.5.0/docs/api/java/lang/String.html">String getRoleDescription(HttpServletRequest request, 1.5.0/docs/api/java/lang/String.html">String role)
    {
        return MessageResources.getMessage(request,ROLE_MESSAGE_RESOURCES_PREFIX + role);
    }

    public static 1.5.0/docs/api/java/lang/String.html">String getSerialRoles(List<String> targetRoles)
    {
        if(targetRoles == null)
            return null;
        1.5.0/docs/api/java/lang/String.html">String roles = null;
        for(1.5.0/docs/api/java/lang/String.html">String role: targetRoles)
        {
            if(roles == null)
                roles = role;
            else
                roles += ROLE_SEPARATOR + role;
        }
        return roles;
    }

    public static List<String> getRolesFromSerial(1.5.0/docs/api/java/lang/String.html">String rolesStr)
    {
         if(rolesStr == null)
            return null;
        1.5.0/docs/api/java/lang/String.html">String[] roles = rolesStr.split(ROLE_SEPARATOR);
        List<String> l = new ArrayList<String>();
        for(1.5.0/docs/api/java/lang/String.html">String role: roles)
        {
            l.add(role);
        }
        return l;
    }

    public static 1.5.0/docs/api/java/lang/String.html">String catRole(1.5.0/docs/api/java/lang/String.html">String old, 1.5.0/docs/api/java/lang/String.html">String  newRole)
    {
        if(old == null || old.length() == 0)
            return newRole;
        else
            return old + ROLE_SEPARATOR + newRole;
    }
}