Search in sources :

Example 21 with Role

use of org.candlepin.model.Role in project candlepin by candlepin.

the class UserResource method getUserRoles.

/*
     * getUserRoles will only return roles for one user. If you want a
     * full view of a role, use /roles/ instead.
     */
@ApiOperation(notes = "Retrieves a list of Roles by User", value = "getUserRoles")
@GET
@Path("/{username}/roles")
@Produces(MediaType.APPLICATION_JSON)
public List<Role> getUserRoles(@PathParam("username") @Verify(User.class) String username) {
    User myUser = userService.findByLogin(username);
    List<Role> roles = new LinkedList<>();
    Set<User> s = new HashSet<>();
    s.add(myUser);
    for (Role r : myUser.getRoles()) {
        // Copy onto a detached role object so we can omit users list, which could
        // technically leak information here.
        Role copy = new Role(r.getName());
        copy.setId(r.getId());
        copy.setPermissions(r.getPermissions());
        copy.setUsers(s);
        roles.add(copy);
    }
    return roles;
}
Also used : Role(org.candlepin.model.Role) User(org.candlepin.model.User) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 22 with Role

use of org.candlepin.model.Role in project candlepin by candlepin.

the class ConsumerResourceCreationTest method init.

@Before
public void init() throws Exception {
    this.i18n = I18nFactory.getI18n(getClass(), Locale.US, I18nFactory.FALLBACK);
    this.modelTranslator = new StandardTranslator(this.consumerTypeCurator, this.environmentCurator, this.ownerCurator);
    testMigration = new GuestMigration(consumerCurator);
    migrationProvider = Providers.of(testMigration);
    this.config = initConfig();
    this.resource = new ConsumerResource(this.consumerCurator, this.consumerTypeCurator, null, this.subscriptionService, this.ownerService, null, this.idCertService, null, this.i18n, this.sink, null, null, null, this.userService, null, null, this.ownerCurator, this.activationKeyCurator, null, this.complianceRules, this.deletedConsumerCurator, null, null, this.config, null, null, null, this.consumerBindUtil, null, null, new FactValidator(this.config, this.i18n), null, consumerEnricher, migrationProvider, modelTranslator);
    this.system = this.initConsumerType();
    this.mockConsumerType(this.system);
    this.systemDto = this.modelTranslator.translate(this.system, ConsumerTypeDTO.class);
    owner = new Owner("test_owner");
    owner.setId(TestUtil.randomString());
    user = new User(USER, "");
    PermissionBlueprint p = new PermissionBlueprint(PermissionType.OWNER, owner, Access.ALL);
    role = new Role();
    role.addPermission(p);
    role.addUser(user);
    when(consumerCurator.create(any(Consumer.class))).thenAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0];
        }
    });
    when(consumerCurator.create(any(Consumer.class), any(Boolean.class))).thenAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0];
        }
    });
    when(userService.findByLogin(USER)).thenReturn(user);
    IdentityCertificate cert = new IdentityCertificate();
    cert.setKey("testKey");
    cert.setCert("testCert");
    cert.setId("testId");
    cert.setSerial(new CertificateSerial(new Date()));
    when(idCertService.generateIdentityCert(any(Consumer.class))).thenReturn(cert);
    when(ownerCurator.lookupByKey(owner.getKey())).thenReturn(owner);
    when(complianceRules.getStatus(any(Consumer.class), any(Date.class), any(Boolean.class), any(Boolean.class))).thenReturn(new ComplianceStatus(new Date()));
}
Also used : FactValidator(org.candlepin.util.FactValidator) Owner(org.candlepin.model.Owner) User(org.candlepin.model.User) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) CertificateSerial(org.candlepin.model.CertificateSerial) StandardTranslator(org.candlepin.dto.StandardTranslator) ConsumerTypeDTO(org.candlepin.dto.api.v1.ConsumerTypeDTO) Date(java.util.Date) Role(org.candlepin.model.Role) GuestMigration(org.candlepin.resource.util.GuestMigration) Answer(org.mockito.stubbing.Answer) Consumer(org.candlepin.model.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PermissionBlueprint(org.candlepin.model.PermissionBlueprint) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) IdentityCertificate(org.candlepin.model.IdentityCertificate) Before(org.junit.Before)

Example 23 with Role

use of org.candlepin.model.Role in project candlepin by candlepin.

the class OwnerResourceUeberCertOperationsTest method setUp.

@Before
public void setUp() {
    owner = ownerCurator.create(new Owner(OWNER_NAME));
    Role ownerAdminRole = createAdminRole(owner);
    roleCurator.create(ownerAdminRole);
    User user = new User("testing user", "pass");
    principal = new UserPrincipal("testing user", new ArrayList<>(permFactory.createPermissions(user, ownerAdminRole.getPermissions())), false);
    setupPrincipal(principal);
    or = new OwnerResource(ownerCurator, productCurator, null, consumerCurator, i18n, null, null, null, null, null, poolManager, null, null, null, null, consumerTypeCurator, entCertCurator, entitlementCurator, ueberCertCurator, ueberCertGenerator, null, null, contentOverrideValidator, serviceLevelValidator, null, null, null, null, null, this.modelTranslator);
}
Also used : Role(org.candlepin.model.Role) Owner(org.candlepin.model.Owner) User(org.candlepin.model.User) ArrayList(java.util.ArrayList) UserPrincipal(org.candlepin.auth.UserPrincipal) Before(org.junit.Before)

Aggregations

Role (org.candlepin.model.Role)23 User (org.candlepin.model.User)14 Test (org.junit.Test)10 PermissionBlueprint (org.candlepin.model.PermissionBlueprint)9 Owner (org.candlepin.model.Owner)8 ApiOperation (io.swagger.annotations.ApiOperation)7 Produces (javax.ws.rs.Produces)7 ApiResponses (io.swagger.annotations.ApiResponses)6 Path (javax.ws.rs.Path)6 HashSet (java.util.HashSet)4 Consumes (javax.ws.rs.Consumes)4 LinkedList (java.util.LinkedList)3 POST (javax.ws.rs.POST)3 UserPrincipal (org.candlepin.auth.UserPrincipal)3 DELETE (javax.ws.rs.DELETE)2 Principal (org.candlepin.auth.Principal)2 OwnerPermission (org.candlepin.auth.permissions.OwnerPermission)2 Permission (org.candlepin.auth.permissions.Permission)2 UsernameConsumersPermission (org.candlepin.auth.permissions.UsernameConsumersPermission)2 NotFoundException (org.candlepin.common.exceptions.NotFoundException)2