/** * Retrieves the current user's roles given the username. * * @param username The username * @return The user roles * @throws DataServiceFault */ public static String[] getUserRoles(String username) throws DataServiceFault { RealmService realmService = DataServicesDSComponent.getRealmService(); RegistryService registryService = DataServicesDSComponent.getRegistryService(); username = MultitenantUtils.getTenantAwareUsername(username); String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); username = MultitenantUtils.getTenantAwareUsername(username); try { if (tenantId < MultitenantConstants.SUPER_TENANT_ID) { tenantId = realmService.getTenantManager().getTenantId(tenantDomain); } if (tenantId < MultitenantConstants.SUPER_TENANT_ID) { /* the tenant doesn't exist. */ log.error("The tenant doesn't exist. Tenant domain:" + tenantDomain); throw new DataServiceFault("Access Denied. You are not authorized."); } if (!realmService.getTenantManager().isTenantActive(tenantId)) { /* the tenant is not active. */ log.error("The tenant is not active. Tenant domain:" + tenantDomain); throw new DataServiceFault("The tenant is not active. Tenant domain:" + tenantDomain); } UserRealm realm = registryService.getUserRealm(tenantId); String roles[] = realm.getUserStoreManager().getRoleListOfUser(username); return roles; } catch (Exception e) { String msg = "Error in retrieving the realm for the tenant id: " + tenantId + ", username: "******". " + e.getMessage(); log.error(msg); throw new DataServiceFault(msg); } }
private String getTenantDomainName() throws KeyStoreMgtException { RealmService realmService = RealmServiceHolder.getRealmService(); if (realmService == null) { String msg = "Error in getting the domain name, realm service is null."; log.error(msg); throw new KeyStoreMgtException(msg); } try { return realmService.getTenantManager().getDomain(tenantId); } catch (org.wso2.carbon.user.api.UserStoreException e) { String msg = "Error in getting the domain name for the tenant id: " + tenantId; log.error(msg, e); throw new KeyStoreMgtException(msg, e); } }
public static TenantManager getTenantManager() { return realmService.getTenantManager(); }
public static void buildMenuItems(HttpServletRequest request, String s, String s1, String s2) { int menuOrder = 50; if (CarbonUIUtil.isUserAuthorized(request, "/permission/admin/manage/resources/ws-api")) { HttpSession session = request.getSession(); String cookie = (String) session.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE); try { WSRegistryServiceClient registry = new WSRegistryServiceClient(s2, cookie); List<GovernanceArtifactConfiguration> configurations = GovernanceUtils.findGovernanceArtifactConfigurations(registry); Map<String, String> customAddUIMap = new LinkedHashMap<String, String>(); Map<String, String> customViewUIMap = new LinkedHashMap<String, String>(); List<Menu> userCustomMenuItemsList = new LinkedList<Menu>(); for (GovernanceArtifactConfiguration configuration : configurations) { Component component = new Component(); OMElement uiConfigurations = configuration.getUIConfigurations(); String key = configuration.getKey(); String configurationPath = RegistryConstants.CONFIG_REGISTRY_BASE_PATH + RegistryConstants.GOVERNANCE_COMPONENT_PATH + "/configuration/"; String layoutStoragePath = configurationPath + key; RealmService realmService = registry.getRegistryContext().getRealmService(); if (realmService .getTenantUserRealm(realmService.getTenantManager().getTenantId(s1)) .getAuthorizationManager() .isUserAuthorized(s, configurationPath, ActionConstants.PUT) || registry.resourceExists(layoutStoragePath)) { List<Menu> menuList = component.getMenusList(); if (uiConfigurations != null) { ComponentBuilder.processMenus("artifactType", uiConfigurations, component); ComponentBuilder.processCustomUIs(uiConfigurations, component); } if (menuList.size() == 0) { // if no menu definitions were present, define the default ones. menuOrder = buildMenuList(request, configuration, menuList, key, menuOrder); } userCustomMenuItemsList.addAll(menuList); customAddUIMap.putAll(component.getCustomAddUIMap()); Map<String, String> viewUIMap = component.getCustomViewUIMap(); if (viewUIMap.isEmpty()) { // if no custom UI definitions were present, define the default. buildViewUI(configuration, viewUIMap, key); } customViewUIMap.putAll(viewUIMap); OMElement layout = configuration.getContentDefinition(); if (layout != null && !registry.resourceExists(layoutStoragePath)) { Resource resource = registry.newResource(); resource.setContent(RegistryUtils.encodeString(layout.toString())); resource.setMediaType("application/xml"); registry.put(layoutStoragePath, resource); } } } session.setAttribute( MenuAdminClient.USER_CUSTOM_MENU_ITEMS, userCustomMenuItemsList.toArray(new Menu[userCustomMenuItemsList.size()])); session.setAttribute("customAddUI", customAddUIMap); session.setAttribute("customViewUI", customViewUIMap); } catch (RegistryException e) { log.error("unable to create connection to registry"); } catch (org.wso2.carbon.user.api.UserStoreException e) { log.error("unable to realm service"); } } }