Search in sources :

Example 6 with Permission

use of org.candlepin.auth.permissions.Permission in project candlepin by candlepin.

the class ConsumerResourceIntegrationTest method setUp.

@Before
public void setUp() {
    standardSystemType = consumerTypeCurator.create(new ConsumerType("standard-system"));
    standardSystemTypeDTO = modelTranslator.translate(standardSystemType, ConsumerTypeDTO.class);
    personType = consumerTypeCurator.create(new ConsumerType(ConsumerTypeEnum.PERSON));
    personTypeDTO = modelTranslator.translate(personType, ConsumerTypeDTO.class);
    owner = ownerCurator.create(new Owner("test-owner"));
    ownerDTO = modelTranslator.translate(owner, OwnerDTO.class);
    owner.setDefaultServiceLevel(DEFAULT_SERVICE_LEVEL);
    ownerCurator.create(owner);
    someuser = userCurator.create(new User(USER_NAME, "dontcare"));
    ownerAdminRole = createAdminRole(owner);
    ownerAdminRole.addUser(someuser);
    roleCurator.create(ownerAdminRole);
    List<Permission> perms = permFactory.createPermissions(someuser, ownerAdminRole.getPermissions());
    principal = new UserPrincipal(USER_NAME, perms, false);
    setupPrincipal(principal);
    consumer = TestUtil.createConsumer(standardSystemType, owner);
    consumerCurator.create(consumer);
    product = TestUtil.createProduct();
    product.setAttribute(Product.Attributes.SUPPORT_LEVEL, DEFAULT_SERVICE_LEVEL);
    productCurator.create(product);
    pool = createPool(owner, product, 10L, TestDateUtil.date(2010, 1, 1), TestDateUtil.date(2020, 12, 31));
}
Also used : Owner(org.candlepin.model.Owner) User(org.candlepin.model.User) OwnerDTO(org.candlepin.dto.api.v1.OwnerDTO) OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) Permission(org.candlepin.auth.permissions.Permission) ConsumerType(org.candlepin.model.ConsumerType) ConsumerTypeDTO(org.candlepin.dto.api.v1.ConsumerTypeDTO) UserPrincipal(org.candlepin.auth.UserPrincipal) Before(org.junit.Before)

Example 7 with Permission

use of org.candlepin.auth.permissions.Permission in project candlepin by candlepin.

the class OwnerInfoCuratorTest method setupOnlyMyConsumersPrincipal.

private User setupOnlyMyConsumersPrincipal() {
    Set<Permission> perms = new HashSet<>();
    User u = new User("MySystemsAdmin", "passwd");
    perms.add(new UsernameConsumersPermission(u, owner));
    Principal p = new UserPrincipal(u.getUsername(), perms, false);
    setupPrincipal(p);
    return u;
}
Also used : UsernameConsumersPermission(org.candlepin.auth.permissions.UsernameConsumersPermission) UsernameConsumersPermission(org.candlepin.auth.permissions.UsernameConsumersPermission) Permission(org.candlepin.auth.permissions.Permission) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) UserPrincipal(org.candlepin.auth.UserPrincipal) HashSet(java.util.HashSet)

Example 8 with Permission

use of org.candlepin.auth.permissions.Permission in project candlepin by candlepin.

the class UserResourceTest method testListAllOwners.

@Test
public void testListAllOwners() {
    User user = new User();
    user.setUsername("dummyuser" + TestUtil.randomInt());
    user.setPassword("password");
    userResource.createUser(user);
    Owner owner1 = createOwner();
    Owner owner2 = createOwner();
    Role owner1Role = new Role(owner1.getKey() + " role");
    Role owner2Role = new Role(owner2.getKey() + " role");
    owner1Role.addPermission(new PermissionBlueprint(PermissionType.OWNER, owner1, Access.ALL));
    owner1Role.addPermission(new PermissionBlueprint(PermissionType.OWNER, owner2, Access.READ_ONLY));
    owner1Role.addUser(user);
    owner2Role.addUser(user);
    roleCurator.create(owner1Role);
    roleCurator.create(owner2Role);
    Set<Permission> perms = new HashSet<>();
    perms.add(new OwnerPermission(owner1, Access.ALL));
    perms.add(new OwnerPermission(owner2, Access.READ_ONLY));
    Principal userPrincipal = new UserPrincipal(user.getUsername(), perms, false);
    // Requesting the list of owners for this user should assume ALL, and not
    // return owner2:
    Iterable<Owner> response = userResource.listUsersOwners(user.getUsername(), userPrincipal);
    List<Owner> owners = new LinkedList<>();
    for (Object entity : response) {
        owners.add((Owner) entity);
    }
    assertEquals(1, owners.size());
    assertEquals(owner1.getKey(), owners.get(0).getKey());
}
Also used : Owner(org.candlepin.model.Owner) OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) User(org.candlepin.model.User) UserPrincipal(org.candlepin.auth.UserPrincipal) LinkedList(java.util.LinkedList) Role(org.candlepin.model.Role) PermissionBlueprint(org.candlepin.model.PermissionBlueprint) UsernameConsumersPermission(org.candlepin.auth.permissions.UsernameConsumersPermission) OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) Permission(org.candlepin.auth.permissions.Permission) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with Permission

use of org.candlepin.auth.permissions.Permission in project candlepin by candlepin.

the class PinsetterAsyncFilterTest method noJobMapPrincipal.

@Test
public void noJobMapPrincipal() {
    List<Permission> permissions = Arrays.asList(new Permission[] { new OwnerPermission(new Owner("test_owner"), Access.ALL) });
    Principal principal = new UserPrincipal("testing", permissions, false);
    when(this.principalProvider.get()).thenReturn(principal);
    JobDetail detail = newJob(RefreshPoolsJob.class).build();
    when(response.getEntity()).thenReturn(detail);
    this.interceptor.postProcess(response);
    Assert.assertEquals(principal, detail.getJobDataMap().get(PinsetterJobListener.PRINCIPAL_KEY));
}
Also used : RefreshPoolsJob(org.candlepin.pinsetter.tasks.RefreshPoolsJob) OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) Owner(org.candlepin.model.Owner) JobDetail(org.quartz.JobDetail) OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) Permission(org.candlepin.auth.permissions.Permission) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) UserPrincipal(org.candlepin.auth.UserPrincipal) Test(org.junit.Test)

Example 10 with Permission

use of org.candlepin.auth.permissions.Permission in project candlepin by candlepin.

the class AbstractHibernateCurator method getSecureCriteriaRestrictions.

/**
 * Builds the criteria restrictions for the given entity class. If the entity does not need any
 * restrictions or the current principal otherwise has full access, this method returns null.
 *
 * @param entityClass
 *  The entity class for which to build secure criteria restrictions
 *
 * @return
 *  the criteria restrictions for the given entity class, or null if no restrictions are
 *  necessary.
 */
protected Criterion getSecureCriteriaRestrictions(Class entityClass) {
    Principal principal = this.principalProvider.get();
    Criterion restrictions = null;
    // access, skip the restriction building
    if (principal != null && !principal.hasFullAccess()) {
        for (Permission permission : principal.getPermissions()) {
            Criterion restriction = permission.getCriteriaRestrictions(entityClass);
            if (restriction != null) {
                log.debug("Adding criteria restriction from permission {} for {}: {}", permission, entityClass, restriction);
                restrictions = (restrictions != null) ? Restrictions.or(restrictions, restriction) : restriction;
            }
        }
    }
    return restrictions;
}
Also used : Criterion(org.hibernate.criterion.Criterion) Permission(org.candlepin.auth.permissions.Permission) Principal(org.candlepin.auth.Principal)

Aggregations

Permission (org.candlepin.auth.permissions.Permission)12 Principal (org.candlepin.auth.Principal)9 UserPrincipal (org.candlepin.auth.UserPrincipal)9 OwnerPermission (org.candlepin.auth.permissions.OwnerPermission)8 Owner (org.candlepin.model.Owner)6 HashSet (java.util.HashSet)5 Test (org.junit.Test)4 UsernameConsumersPermission (org.candlepin.auth.permissions.UsernameConsumersPermission)3 User (org.candlepin.model.User)3 LinkedList (java.util.LinkedList)2 PermissionBlueprint (org.candlepin.model.PermissionBlueprint)2 Role (org.candlepin.model.Role)2 JobDetail (org.quartz.JobDetail)2 ArrayList (java.util.ArrayList)1 XmlTransient (javax.xml.bind.annotation.XmlTransient)1 NoAuthPrincipal (org.candlepin.auth.NoAuthPrincipal)1 TrustedUserPrincipal (org.candlepin.auth.TrustedUserPrincipal)1 PermissionFactory (org.candlepin.auth.permissions.PermissionFactory)1 ConsumerTypeDTO (org.candlepin.dto.api.v1.ConsumerTypeDTO)1 OwnerDTO (org.candlepin.dto.api.v1.OwnerDTO)1