1 package com.dotmarketing.factories; 2 3 import java.util.HashSet ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.Set ; 7 8 import org.apache.axis.components.logger.LogFactory; 9 10 import com.dotmarketing.cms.factories.PublicCompanyFactory; 11 import com.dotmarketing.util.Config; 12 import com.dotmarketing.util.Logger; 13 import com.liferay.portal.NoSuchRoleException; 14 import com.liferay.portal.ejb.GroupLocalManagerUtil; 15 import com.liferay.portal.ejb.RoleLocalManagerUtil; 16 import com.liferay.portal.ejb.UserLocalManagerUtil; 17 import com.liferay.portal.model.Group; 18 import com.liferay.portal.model.Role; 19 import com.liferay.portal.model.User; 20 21 25 public class RoleFactory { 26 27 private static Role _cmsAdministratorRole = null; 28 29 33 public static void createDefaultRoles() { 34 61 } 62 63 public static Role getRoleByName(String roleName) { 64 Role role = new Role(); 65 try { 66 role = RoleLocalManagerUtil.getRoleByName(com.dotmarketing.cms.factories.PublicCompanyFactory 67 .getDefaultCompany().getCompanyId(), roleName); 68 } catch (Exception e) { 69 Logger.warn(RoleFactory.class, "getRoleByName: Errors getting role: " + roleName); 70 } 71 return role; 72 } 73 74 public static Role getRoleById(String roleId) throws NoSuchRoleException { 75 Role role = new Role(); 76 try { 77 role = RoleLocalManagerUtil.getRoleById(roleId); 78 } catch (NoSuchRoleException e) { 79 Logger.warn(RoleFactory.class, "getRoleById: Errors getting role: " + roleId, e); 80 throw e; 81 } catch (Exception e) { 82 Logger.warn(RoleFactory.class, "getRoleById: Errors getting role: " + roleId, e); 83 } 84 return role; 85 } 86 87 90 @SuppressWarnings ("unchecked") 91 public static List <Role> getAllRolesForUser(String userId) { 92 List <Role> myRoles = null; 93 List <Group> myGroups = null; 94 95 try { 96 myRoles = UserLocalManagerUtil.getRoles(userId); 97 } catch (Exception e) { 98 } 99 100 try { 102 myGroups = UserLocalManagerUtil.getGroups(userId); 103 } catch (Exception e) { 104 } 105 106 Iterator i = myGroups.iterator(); 108 while (i.hasNext()) { 109 Group g = (Group) i.next(); 110 try { 112 List <Role> groupRoles = GroupLocalManagerUtil.getRoles(g.getGroupId()); 113 myRoles.addAll(groupRoles); 114 } catch (Exception e) { 115 } 116 117 } 118 return myRoles; 119 120 } 121 122 125 @SuppressWarnings ("unchecked") 126 public static Set <User> getUsers(String roleId) { 127 Set <User> users = new HashSet <User>(); 128 129 try { 130 users.addAll(RoleLocalManagerUtil.getUsers(roleId)); 131 } catch (Exception e) { 132 } 133 134 try { 136 List <Group> groups = (List <Group>) RoleLocalManagerUtil.getGroups(roleId); 137 for (Group group : groups) { 138 users.addAll(GroupLocalManagerUtil.getUsers(group.getGroupId())); 139 } 140 } catch (Exception e) { 141 } 142 return users; 143 144 } 145 146 public static Role getCMSAdministratorRole() { 147 if (_cmsAdministratorRole == null) { 148 try { 149 String companyId = PublicCompanyFactory.getDefaultCompanyId(); 150 _cmsAdministratorRole = RoleLocalManagerUtil.getRoleByName(companyId, Config 151 .getStringProperty("CMS_ADMINISTRATOR_ROLE")); 152 } catch (Exception e) { 153 LogFactory.getLog("RoleFactory").error("Can't find CMS_ADMINISTRATOR_ROLE !!!"); 154 e.printStackTrace(); 155 } 156 157 } 158 return _cmsAdministratorRole; 159 160 } 161 162 public static boolean userHasRole(User user, String roleName) { 163 164 Iterator rolesIt = getAllRolesForUser(user.getUserId()).iterator(); 165 while (rolesIt.hasNext()) { 166 Role role = (Role) rolesIt.next(); 167 if (role.getName().equals(roleName)) { 168 return true; 169 } 170 } 171 return false; 172 } 173 } | Popular Tags |