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));
}
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));
}
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();
}
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);
}
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();
}
Aggregations