Search in sources :

Example 11 with SUserGroup

use of jetbrains.buildServer.groups.SUserGroup in project teamcity-rest by JetBrains.

the class GroupRequestTest method testUsers.

@Test
void testUsers() {
    SUserGroup group1 = myFixture.createUserGroup("key1", "name1", "description");
    final SUser user1 = createUser("user1");
    final SUser user2 = createUser("user2");
    group1.addUser(user1);
    group1.addUser(user2);
    SUserGroup group2 = myFixture.createUserGroup("key2", "name2", "description");
    final SUser user3 = createUser("user3");
    group2.addUser(user3);
    {
        Group result = myRequest.serveGroup("key:key1", "$long,users($long)");
        assertNotNull(result.users);
        assertNotNull(result.users.users);
        assertEquals(2, result.users.users.size());
        assertEquals(Long.valueOf(user1.getId()), result.users.users.get(0).getId());
        assertEquals(Long.valueOf(user2.getId()), result.users.users.get(1).getId());
    }
}
Also used : Group(jetbrains.buildServer.server.rest.model.group.Group) UserGroup(jetbrains.buildServer.groups.UserGroup) SUserGroup(jetbrains.buildServer.groups.SUserGroup) SUser(jetbrains.buildServer.users.SUser) SUserGroup(jetbrains.buildServer.groups.SUserGroup) BaseFinderTest(jetbrains.buildServer.server.rest.data.BaseFinderTest) Test(org.testng.annotations.Test)

Example 12 with SUserGroup

use of jetbrains.buildServer.groups.SUserGroup in project teamcity-rest by JetBrains.

the class UserRequestTest method testUserEnityExposure.

@Test
void testUserEnityExposure() throws Throwable {
    myFixture.getServerSettings().setPerProjectPermissionsEnabled(true);
    final SUser user1 = createUser("user1");
    final SUser user2 = createUser("user2");
    // filling all user fields
    user1.updateUserAccount("user1", "Display Name1", "email1@domain.com");
    user2.updateUserAccount("user2", "Display Name2", "email2@domain.com");
    SUserGroup group1 = myFixture.createUserGroup("key1", "name1", "description");
    group1.addUser(user1);
    group1.addUser(user2);
    user1.addRole(RoleScope.globalScope(), getProjectViewerRole());
    user2.addRole(RoleScope.globalScope(), getProjectViewerRole());
    user1.setLastLoginTimestamp(new Date());
    user2.setLastLoginTimestamp(new Date());
    user1.setPassword("secret");
    user2.setPassword("secret");
    myFixture.getUserAvatarsManager().saveAvatar(user1, new BufferedImage(1, 1, 1));
    myFixture.getUserAvatarsManager().saveAvatar(user2, new BufferedImage(1, 1, 1));
    enable2FA(user1);
    enable2FA(user2);
    SecurityContextImpl securityContext = myFixture.getSecurityContext();
    user2.addRole(RoleScope.globalScope(), getProjectAdminRole());
    SFinishedBuild build10 = build().in(myBuildType).by(user1).finish();
    SFinishedBuild build20 = build().in(myBuildType).by(user2).finish();
    BuildRequest buildRequest = new BuildRequest();
    buildRequest.initForTests(BaseFinderTest.getBeanContext(myFixture));
    // if changed, the checks below should be changed
    assertEquals(15, getSubEntitiesNames(User.class).size());
    final String fields = "triggered(user($long,hasPassword))";
    {
        Build build = buildRequest.serveBuild("id:" + build10.getBuildId(), fields, new FakeHttpServletRequest());
        // check that all is present
        User user = build.getTriggered().user;
        assertNotNull(user.getUsername());
        assertNotNull(user.getName());
        assertNotNull(user.getId());
        assertNotNull(user.getEmail());
        assertNotNull(user.getLastLogin());
        assertNotNull(user.getHref());
        assertNotNull(user.getProperties());
        assertNotNull(user.getRoles());
        assertNotNull(user.getGroups());
        assertNotNull(user.getHasPassword());
        assertNotNull(user.getAvatars());
        assertNotNull(user.getEnabled2FA());
        // not included in response
        assertNull(user.getPassword());
        // submit-only
        assertNull(user.getLocator());
        // obsolete
        assertNull(user.getRealm());
    }
    {
        Build build = buildRequest.serveBuild("id:" + build20.getBuildId(), fields, new FakeHttpServletRequest());
        // check that all is present
        User user = build.getTriggered().user;
        assertNotNull(user.getUsername());
        assertNotNull(user.getName());
        assertNotNull(user.getId());
        assertNotNull(user.getEmail());
        assertNotNull(user.getLastLogin());
        assertNotNull(user.getHref());
        assertNotNull(user.getProperties());
        assertNotNull(user.getRoles());
        assertNotNull(user.getGroups());
        assertNotNull(user.getHasPassword());
        assertNotNull(user.getAvatars());
        assertNotNull(user.getEnabled2FA());
        // not included in response
        assertNull(user.getPassword());
        // submit-only
        assertNull(user.getLocator());
        // obsolete
        assertNull(user.getRealm());
    }
    securityContext.runAs(user1, () -> {
        Build build = buildRequest.serveBuild("id:" + build10.getBuildId(), fields, new FakeHttpServletRequest());
        // check that all is present
        User user = build.getTriggered().user;
        assertNotNull(user.getUsername());
        assertNotNull(user.getName());
        assertNotNull(user.getId());
        assertNotNull(user.getEmail());
        assertNotNull(user.getLastLogin());
        assertNotNull(user.getHref());
        assertNotNull(user.getProperties());
        assertNotNull(user.getRoles());
        assertNotNull(user.getGroups());
        assertNotNull(user.getHasPassword());
        assertNotNull(user.getAvatars());
        assertNotNull(user.getEnabled2FA());
        assertNull(user.getPassword());
    });
    securityContext.runAs(user2, () -> {
        Build build = buildRequest.serveBuild("id:" + build10.getBuildId(), fields, new FakeHttpServletRequest());
        // check that all is present
        User user = build.getTriggered().user;
        assertNotNull(user.getUsername());
        assertNotNull(user.getName());
        assertNotNull(user.getId());
        assertNotNull(user.getEmail());
        assertNotNull(user.getLastLogin());
        assertNotNull(user.getHref());
        assertNotNull(user.getProperties());
        assertNotNull(user.getRoles());
        assertNotNull(user.getGroups());
        assertNotNull(user.getHasPassword());
        assertNotNull(user.getAvatars());
        assertNotNull(user.getEnabled2FA());
        assertNull(user.getPassword());
    });
    securityContext.runAs(user1, () -> {
        Build build = buildRequest.serveBuild("id:" + build20.getBuildId(), fields, new FakeHttpServletRequest());
        // check that all is present
        User user = build.getTriggered().user;
        assertNotNull(user.getUsername());
        assertNotNull(user.getName());
        assertNotNull(user.getId());
        assertNull(user.getEmail());
        assertNull(user.getLastLogin());
        assertNotNull(user.getHref());
        assertNull(user.getProperties());
        assertNull(user.getRoles());
        assertNull(user.getGroups());
        assertNull(user.getHasPassword());
        assertNull(user.getPassword());
        assertNotNull(user.getAvatars());
        assertNull(user.getEnabled2FA());
    });
}
Also used : SFinishedBuild(jetbrains.buildServer.serverSide.SFinishedBuild) SecurityContextImpl(jetbrains.buildServer.serverSide.impl.auth.SecurityContextImpl) FakeHttpServletRequest(jetbrains.buildServer.controllers.fakes.FakeHttpServletRequest) User(jetbrains.buildServer.server.rest.model.user.User) SUser(jetbrains.buildServer.users.SUser) Build(jetbrains.buildServer.server.rest.model.build.Build) SFinishedBuild(jetbrains.buildServer.serverSide.SFinishedBuild) SUser(jetbrains.buildServer.users.SUser) SUserGroup(jetbrains.buildServer.groups.SUserGroup) BufferedImage(java.awt.image.BufferedImage) Test(org.testng.annotations.Test) BaseFinderTest(jetbrains.buildServer.server.rest.data.BaseFinderTest)

Example 13 with SUserGroup

use of jetbrains.buildServer.groups.SUserGroup in project teamcity-rest by JetBrains.

the class UserRequest method removeGroup.

@DELETE
@Path("/{userLocator}/groups/{groupLocator}")
@ApiOperation(value = "Remove the matching user from the specific group.", nickname = "removeUserFromGroup")
public void removeGroup(@ApiParam(format = LocatorName.USER) @PathParam("userLocator") String userLocator, @PathParam("groupLocator") String groupLocator, @QueryParam("fields") String fields) {
    if (TeamCityProperties.getBooleanOrTrue(UserFinder.REST_CHECK_ADDITIONAL_PERMISSIONS_ON_USERS_AND_GROUPS)) {
        myUserFinder.checkViewAllUsersPermission();
    }
    SUser user = myUserFinder.getItem(userLocator, true);
    SUserGroup group = myBeanContext.getSingletonService(UserGroupFinder.class).getGroup(groupLocator);
    if (!user.getUserGroups().contains(group)) {
        throw new NotFoundException("User does not belong to the group");
    }
    group.removeUser(user);
}
Also used : SUser(jetbrains.buildServer.users.SUser) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) SUserGroup(jetbrains.buildServer.groups.SUserGroup) ApiOperation(io.swagger.annotations.ApiOperation)

Example 14 with SUserGroup

use of jetbrains.buildServer.groups.SUserGroup in project teamcity-rest by JetBrains.

the class UserRequest method addGroupToUser.

@POST
@Path("/{userLocator}/groups")
@Consumes({ "application/xml", "application/json" })
@Produces({ "application/xml", "application/json" })
@ApiOperation(value = "Add a user matching the locator to the group.", nickname = "addUserToGroup", hidden = true)
public Group addGroupToUser(@ApiParam(format = LocatorName.USER) @PathParam("userLocator") String userLocator, Group group, @QueryParam("fields") String fields) {
    SUser user = myUserFinder.getItem(userLocator, true);
    SUserGroup userGroup = group.getFromPosted(myBeanContext.getServiceLocator());
    userGroup.addUser(user);
    return new Group(userGroup, new Fields(fields), myBeanContext);
}
Also used : SUserGroup(jetbrains.buildServer.groups.SUserGroup) Group(jetbrains.buildServer.server.rest.model.group.Group) Fields(jetbrains.buildServer.server.rest.model.Fields) SUser(jetbrains.buildServer.users.SUser) SUserGroup(jetbrains.buildServer.groups.SUserGroup) ApiOperation(io.swagger.annotations.ApiOperation)

Example 15 with SUserGroup

use of jetbrains.buildServer.groups.SUserGroup in project teamcity-rest by JetBrains.

the class UserRequest method getGroup.

@GET
@Path("/{userLocator}/groups/{groupLocator}")
@Produces({ "application/xml", "application/json" })
@ApiOperation(value = "Get a user group of the matching user.", nickname = "getUserGroup")
public Group getGroup(@ApiParam(format = LocatorName.USER) @PathParam("userLocator") String userLocator, @PathParam("groupLocator") String groupLocator, @QueryParam("fields") String fields) {
    if (TeamCityProperties.getBooleanOrTrue(UserFinder.REST_CHECK_ADDITIONAL_PERMISSIONS_ON_USERS_AND_GROUPS)) {
        myUserFinder.checkViewAllUsersPermission();
    }
    SUser user = myUserFinder.getItem(userLocator, true);
    SUserGroup group = myBeanContext.getSingletonService(UserGroupFinder.class).getGroup(groupLocator);
    if (!user.getUserGroups().contains(group)) {
        throw new NotFoundException("User does not belong to the group");
    }
    return new Group(group, new Fields(fields), myBeanContext);
}
Also used : SUserGroup(jetbrains.buildServer.groups.SUserGroup) Group(jetbrains.buildServer.server.rest.model.group.Group) Fields(jetbrains.buildServer.server.rest.model.Fields) SUser(jetbrains.buildServer.users.SUser) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) SUserGroup(jetbrains.buildServer.groups.SUserGroup) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

SUserGroup (jetbrains.buildServer.groups.SUserGroup)18 SUser (jetbrains.buildServer.users.SUser)11 ApiOperation (io.swagger.annotations.ApiOperation)9 Test (org.testng.annotations.Test)7 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)4 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)4 Fields (jetbrains.buildServer.server.rest.model.Fields)4 Group (jetbrains.buildServer.server.rest.model.group.Group)3 RoleAssignment (jetbrains.buildServer.server.rest.model.user.RoleAssignment)3 Permissions (jetbrains.buildServer.serverSide.auth.Permissions)3 ProjectEx (jetbrains.buildServer.serverSide.impl.ProjectEx)3 RoleImpl (jetbrains.buildServer.serverSide.impl.auth.RoleImpl)3 BaseFinderTest (jetbrains.buildServer.server.rest.data.BaseFinderTest)2 RoleAssignments (jetbrains.buildServer.server.rest.model.user.RoleAssignments)2 StringUtil (com.intellij.openapi.util.text.StringUtil)1 BufferedImage (java.awt.image.BufferedImage)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1