Search in sources :

Example 46 with SUser

use of jetbrains.buildServer.users.SUser in project teamcity-rest by JetBrains.

the class TwoFactorRequestTest method testUserCanDeleteHisOwnKey.

@Test
public void testUserCanDeleteHisOwnKey() throws Throwable {
    final SUser user = createUser("user");
    myManager.setSecretKey(user, "secret");
    Set<String> recovery = new HashSet<>(Arrays.asList("a", "b"));
    myManager.setRecoveryKeys(user, recovery);
    myFixture.getSecurityContext().runAs(user, new SecurityContextEx.RunAsAction() {

        @Override
        public void run() {
            myRequest.deleteTwoFactor("username:user");
        }
    });
    assertNull(user.getPropertyValue(SECRET_KEY_PROPERTY));
    assertNull(user.getPropertyValue(RECOVERY_KEY_PROPERTY));
}
Also used : SecurityContextEx(jetbrains.buildServer.serverSide.SecurityContextEx) SUser(jetbrains.buildServer.users.SUser) HashSet(java.util.HashSet) BaseFinderTest(jetbrains.buildServer.server.rest.data.BaseFinderTest) Test(org.testng.annotations.Test)

Example 47 with SUser

use of jetbrains.buildServer.users.SUser in project teamcity-rest by JetBrains.

the class TwoFactorRequestTest method testAdminCanDeleteUserKey.

@Test
public void testAdminCanDeleteUserKey() throws Throwable {
    final SUser user = createUser("user");
    myManager.setSecretKey(user, "secret");
    final SUser admin = createAdmin("admin");
    myFixture.getSecurityContext().runAs(admin, new SecurityContextEx.RunAsAction() {

        @Override
        public void run() {
            myRequest.deleteTwoFactor("username:user");
        }
    });
    assertNull(user.getPropertyValue(SECRET_KEY_PROPERTY));
}
Also used : SecurityContextEx(jetbrains.buildServer.serverSide.SecurityContextEx) SUser(jetbrains.buildServer.users.SUser) BaseFinderTest(jetbrains.buildServer.server.rest.data.BaseFinderTest) Test(org.testng.annotations.Test)

Example 48 with SUser

use of jetbrains.buildServer.users.SUser in project teamcity-rest by JetBrains.

the class AvatarRequest method getAvatarWithHash.

@GET
@Produces(MediaType.IMAGE_PNG_VALUE)
@Path("/{userLocator}/{size}/avatar.{hash}.png")
@ApiOperation("Get a users avatar")
public Response getAvatarWithHash(@Context HttpServletResponse response, @ApiParam(format = LocatorName.USER) @PathParam("userLocator") String userLocator, @ApiParam(value = "avatar's size", allowableValues = "range[" + MIN_AVATAR_SIZE + ", " + MAX_AVATAR_SIZE + "]") @PathParam("size") Integer size, @PathParam("hash") String hash) throws IOException {
    if (size < MIN_AVATAR_SIZE || size > MAX_AVATAR_SIZE) {
        throw new BadRequestException("\"size\" must be bigger or equal than " + MIN_AVATAR_SIZE + " and lower or equal than " + MAX_AVATAR_SIZE);
    }
    final SUser user = myUserFinder.getItem(userLocator);
    if (!hash.equals(user.getPropertyValue(AVATAR_HASH)))
        throw new NotFoundException("Avatar with hash - " + hash + " not found");
    final BufferedImage image = myUserAvatarsManager.getAvatar(user, size);
    if (image == null)
        throw new NotFoundException("avatar (username: " + user.getUsername() + ") not found");
    response.setHeader(HttpHeaders.CACHE_CONTROL, CACHE_CONTROL_MAX_AGE + CACHE_CONTROL_NEVER_EXPIRES);
    ImageIO.write(image, "png", response.getOutputStream());
    return Response.ok().build();
}
Also used : SUser(jetbrains.buildServer.users.SUser) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) BufferedImage(java.awt.image.BufferedImage) ApiOperation(io.swagger.annotations.ApiOperation)

Example 49 with SUser

use of jetbrains.buildServer.users.SUser in project teamcity-rest by JetBrains.

the class AvatarRequest method putAvatar.

@PUT
@Path("/{userLocator}")
@Consumes(MediaType.MULTIPART_FORM_DATA_VALUE)
@ApiOperation("Update a users avatar")
public void putAvatar(@Context HttpServletRequest request, @FormDataParam("avatar") InputStream avatar, @ApiParam(format = LocatorName.USER) @PathParam("userLocator") String userLocator) throws IOException {
    final SUser currentUser = myUserFinder.getCurrentUser();
    if (currentUser == null)
        throw new AccessDeniedException(null, "Log in to your account");
    final SUser targetUser = myUserFinder.getItem(userLocator);
    ServerAuthUtil.canEditUser(currentUser, targetUser);
    // check avatar file size
    final long avatarMaxSize = getAvatarMaxSize();
    if (request.getContentLength() >= avatarMaxSize) {
        throw new BadRequestException(String.format("The size of the avatar must be less than or equal to %d kilobytes (%d bytes)", avatarMaxSize / 1024, avatarMaxSize));
    }
    final BufferedImage image = ImageIO.read(avatar);
    myUserAvatarsManager.saveAvatar(targetUser, image);
}
Also used : AccessDeniedException(jetbrains.buildServer.serverSide.auth.AccessDeniedException) SUser(jetbrains.buildServer.users.SUser) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) BufferedImage(java.awt.image.BufferedImage) ApiOperation(io.swagger.annotations.ApiOperation)

Example 50 with SUser

use of jetbrains.buildServer.users.SUser in project teamcity-rest by JetBrains.

the class AvatarRequest method deleteAvatar.

@DELETE
@Path("/{userLocator}")
@ApiOperation("Delete a users avatar")
public Response deleteAvatar(@ApiParam(format = LocatorName.USER) @PathParam("userLocator") String userLocator) throws IOException {
    final SUser currentUser = myUserFinder.getCurrentUser();
    if (currentUser == null)
        throw new AccessDeniedException(null, "Log in to your account");
    final SUser targetUser = myUserFinder.getItem(userLocator);
    ServerAuthUtil.canEditUser(currentUser, targetUser);
    myUserAvatarsManager.deleteAvatar(targetUser);
    return Response.noContent().build();
}
Also used : AccessDeniedException(jetbrains.buildServer.serverSide.auth.AccessDeniedException) SUser(jetbrains.buildServer.users.SUser) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

SUser (jetbrains.buildServer.users.SUser)125 Test (org.testng.annotations.Test)70 ApiOperation (io.swagger.annotations.ApiOperation)35 BaseFinderTest (jetbrains.buildServer.server.rest.data.BaseFinderTest)35 Build (jetbrains.buildServer.server.rest.model.build.Build)20 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)18 NotNull (org.jetbrains.annotations.NotNull)18 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)15 SecurityContextEx (jetbrains.buildServer.serverSide.SecurityContextEx)13 Fields (jetbrains.buildServer.server.rest.model.Fields)12 SUserGroup (jetbrains.buildServer.groups.SUserGroup)11 ProjectEx (jetbrains.buildServer.serverSide.impl.ProjectEx)10 ServiceLocator (jetbrains.buildServer.ServiceLocator)9 BuildTypeImpl (jetbrains.buildServer.serverSide.impl.BuildTypeImpl)9 Nullable (org.jetbrains.annotations.Nullable)9 LocatorProcessException (jetbrains.buildServer.server.rest.errors.LocatorProcessException)8 SecurityContextImpl (jetbrains.buildServer.serverSide.impl.auth.SecurityContextImpl)8 java.util (java.util)7 Collectors (java.util.stream.Collectors)7 jetbrains.buildServer.serverSide (jetbrains.buildServer.serverSide)7