package pt.estgp.estgweb.utils;
import jomm.utils.MessageResources;
import pt.estgp.estgweb.domain.GenericUser;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* @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 boolean hasRole
(List
<String
> roles,
1.5.0/docs/api/java/lang/String.html">String requiredRole
)
{
if(roles ==
null || roles.
size() == 0
)
return false;
for(1.5.0/docs/api/java/lang/String.html">String role: roles
)
{
if(role.
equals(requiredRole
))
return true;
}
return false;
}
/*public static 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
)
{
return getSerialStringsForRoles
(targetRoles
);
}
public static List
<String
> getRolesFromSerial
(1.5.0/docs/api/java/lang/String.html">String rolesStr
)
{
return StringsUtils.
getStringsFromSerial(rolesStr,ROLE_SEPARATOR
);
}
public static 1.5.0/docs/api/java/lang/String.html">String addRole
(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
{
List
<String
> oldRoles = getRolesFromSerial
(old
);
if(!oldRoles.
contains(newRole
))
oldRoles.
add(newRole
);
else
return old
;
return getSerialRoles
(oldRoles
);
}
}
public static 1.5.0/docs/api/java/lang/String.html">String removeRole
(1.5.0/docs/api/java/lang/String.html">String old,
1.5.0/docs/api/java/lang/String.html">String toRemoveRole
)
{
if(old ==
null || old.
length() == 0
)
return "";
else
{
List
<String
> oldRoles = getRolesFromSerial
(old
);
while(oldRoles.
contains(toRemoveRole
))
oldRoles.
remove(toRemoveRole
);
return getSerialRoles
(oldRoles
);
}
}
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
;
}
public static 1.5.0/docs/api/java/lang/String.html">String getSerialStringsForRoles
(List
<String
> strings
)
{
if (strings ==
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 : strings
)
{
if (roles ==
null)
roles = role + ROLE_SEPARATOR
;
else
roles += role + ROLE_SEPARATOR
;
}
return roles
;
}
}