Search in sources :

Example 11 with Tenant

use of org.craftercms.profile.api.Tenant in project profile by craftercms.

the class TenantServiceImplTest method getTenant2.

private Tenant getTenant2() {
    Tenant tenant = new Tenant();
    tenant.setId(TENANT2_ID);
    tenant.setName(TENANT2_NAME);
    tenant.setVerifyNewProfiles(true);
    tenant.setAvailableRoles(SetUtils.asSet(ROLE1, ROLE2));
    tenant.setAttributeDefinitions(new ArrayList<>(Collections.singletonList(getAttribute1Definition())));
    return tenant;
}
Also used : Tenant(org.craftercms.profile.api.Tenant)

Example 12 with Tenant

use of org.craftercms.profile.api.Tenant in project profile by craftercms.

the class TenantServiceImplTest method testUpdateTenant.

@Test
public void testUpdateTenant() throws Exception {
    AttributeDefinition def1 = new AttributeDefinition();
    def1.setName(ATTRIB1_NAME);
    AttributeDefinition def2 = new AttributeDefinition();
    def2.setName(ATTRIB2_NAME);
    def2.setDefaultValue(DEFAULT_ATTRIB_VALUE);
    Tenant expected = getTenant1();
    expected.getAvailableRoles().remove(ROLE1);
    expected.getAttributeDefinitions().remove(def1);
    expected.getAttributeDefinitions().add(def2);
    Map<String, Object> expectedSetParams = new HashMap<>();
    expectedSetParams.put("verifyNewProfiles", expected.isVerifyNewProfiles());
    expectedSetParams.put("availableRoles", expected.getAvailableRoles());
    expectedSetParams.put("ssoEnabled", expected.isSsoEnabled());
    expectedSetParams.put("attributeDefinitions", expected.getAttributeDefinitions());
    Tenant actual = tenantService.updateTenant(expected);
    assertEqualTenants(expected, actual);
    verify(profileRepository).removeRoleFromAll(TENANT1_NAME, ROLE1);
    verify(profileRepository).removeAttributeFromAll(TENANT1_NAME, ATTRIB1_NAME);
    verify(profileRepository).updateAllWithDefaultValue(TENANT1_NAME, ATTRIB2_NAME, DEFAULT_ATTRIB_VALUE);
    verify(tenantRepository).update(TENANT1_ID.toString(), "{$set: #}", false, false, expectedSetParams);
}
Also used : Tenant(org.craftercms.profile.api.Tenant) HashMap(java.util.HashMap) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

Example 13 with Tenant

use of org.craftercms.profile.api.Tenant in project profile by craftercms.

the class TenantController method getTenant.

@RequestMapping(value = URL_GET_TENANT, method = RequestMethod.GET)
@ResponseBody
public Tenant getTenant(@PathVariable(PATH_VAR_NAME) String name) throws ProfileException {
    checkIfAllowed(name, Action.GET_TENANT);
    Tenant tenant = tenantService.getTenant(name);
    if (tenant != null) {
        return tenant;
    } else {
        throw new ResourceNotFoundException("No tenant found with name '" + name + "'");
    }
}
Also used : Tenant(org.craftercms.profile.api.Tenant) ResourceNotFoundException(org.craftercms.profile.management.exceptions.ResourceNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 14 with Tenant

use of org.craftercms.profile.api.Tenant in project profile by craftercms.

the class TenantController method updateTenant.

@RequestMapping(value = URL_UPDATE_TENANT, method = RequestMethod.POST)
@ResponseBody
public Map<String, String> updateTenant(@RequestBody Tenant tenant) throws ProfileException {
    String name = tenant.getName();
    checkIfAllowed(name, Action.UPDATE_TENANT);
    Tenant currentTenant = tenantService.getTenant(name);
    if (currentTenant != null) {
        if (!currentTenant.getAvailableRoles().contains(AuthorizationUtils.SUPERADMIN_ROLE) && tenant.getAvailableRoles().contains(AuthorizationUtils.SUPERADMIN_ROLE)) {
            throw new ActionDeniedException(Action.UPDATE_TENANT.toString(), name);
        }
        if (currentTenant.getAvailableRoles().contains(AuthorizationUtils.SUPERADMIN_ROLE) && !tenant.getAvailableRoles().contains(AuthorizationUtils.SUPERADMIN_ROLE)) {
            throw new ActionDeniedException(Action.UPDATE_TENANT.toString(), name);
        }
        tenantService.updateTenant(tenant);
        return Collections.singletonMap(MODEL_MESSAGE, String.format(MSG_TENANT_UPDATED_FORMAT, name));
    } else {
        throw new ResourceNotFoundException("No tenant found with name '" + name + "'");
    }
}
Also used : Tenant(org.craftercms.profile.api.Tenant) ActionDeniedException(org.craftercms.commons.security.exception.ActionDeniedException) ResourceNotFoundException(org.craftercms.profile.management.exceptions.ResourceNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 15 with Tenant

use of org.craftercms.profile.api.Tenant in project profile by craftercms.

the class TenantController method deleteTenant.

@RequestMapping(value = URL_DELETE_TENANT, method = RequestMethod.POST)
@ResponseBody
public Map<String, String> deleteTenant(@PathVariable(PATH_VAR_NAME) String name) throws ProfileException {
    checkIfAllowed(name, Action.DELETE_TENANT);
    Tenant tenant = tenantService.getTenant(name);
    if (tenant != null) {
        tenantService.deleteTenant(name);
        return Collections.singletonMap(MODEL_MESSAGE, String.format(MSG_TENANT_DELETED_FORMAT, name));
    } else {
        throw new ResourceNotFoundException("No tenant found with name '" + name + "'");
    }
}
Also used : Tenant(org.craftercms.profile.api.Tenant) ResourceNotFoundException(org.craftercms.profile.management.exceptions.ResourceNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Tenant (org.craftercms.profile.api.Tenant)46 Test (org.junit.Test)21 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)12 AttributeDefinition (org.craftercms.profile.api.AttributeDefinition)8 TenantUpdater (org.craftercms.profile.utils.db.TenantUpdater)8 HashMap (java.util.HashMap)7 ProfileException (org.craftercms.profile.api.exceptions.ProfileException)7 Mockito.anyString (org.mockito.Mockito.anyString)7 MongoDataException (org.craftercms.commons.mongo.MongoDataException)5 Profile (org.craftercms.profile.api.Profile)5 ResourceNotFoundException (org.craftercms.profile.management.exceptions.ResourceNotFoundException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 HashSet (java.util.HashSet)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 DuplicateKeyException (org.craftercms.commons.mongo.DuplicateKeyException)1 UpdateHelper (org.craftercms.commons.mongo.UpdateHelper)1