Search in sources :

Example 1 with UserProfile

use of org.sagebionetworks.repo.model.UserProfile in project Synapse-Repository-Services by Sage-Bionetworks.

the class UserProfileServiceImpl method updateUserProfile.

@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public UserProfile updateUserProfile(String userId, HttpHeaders header, String etag, HttpServletRequest request) throws NotFoundException, ConflictingUpdateException, DatastoreException, InvalidModelException, UnauthorizedException, IOException {
    UserInfo userInfo = userManager.getUserInfo(userId);
    UserProfile entity = (UserProfile) objectTypeSerializer.deserialize(request.getInputStream(), header, UserProfile.class, header.getContentType());
    if (etag != null) {
        entity.setEtag(etag.toString());
    }
    return userProfileManager.updateUserProfile(userInfo, entity);
}
Also used : UserProfile(org.sagebionetworks.repo.model.UserProfile) UserInfo(org.sagebionetworks.repo.model.UserInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserProfile

use of org.sagebionetworks.repo.model.UserProfile in project Synapse-Repository-Services by Sage-Bionetworks.

the class DBOUserProfileDAOImpl method get.

@Override
public UserProfile get(String id, ObjectSchema schema) throws DatastoreException, NotFoundException {
    MapSqlParameterSource param = new MapSqlParameterSource();
    param.addValue(DBOUserProfile.OWNER_ID_FIELD_NAME, id);
    DBOUserProfile jdo = basicDao.getObjectById(DBOUserProfile.class, param);
    UserProfile dto = new UserProfile();
    UserProfileUtils.copyDboToDto(jdo, dto, schema);
    return dto;
}
Also used : DBOUserProfile(org.sagebionetworks.repo.model.dbo.persistence.DBOUserProfile) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) UserProfile(org.sagebionetworks.repo.model.UserProfile) DBOUserProfile(org.sagebionetworks.repo.model.dbo.persistence.DBOUserProfile)

Example 3 with UserProfile

use of org.sagebionetworks.repo.model.UserProfile in project Synapse-Repository-Services by Sage-Bionetworks.

the class DBOUserProfileDAOImpl method update.

/**
 * @param fromBackup Whether we are updating from backup.
 *                   Skip optimistic locking and accept the backup e-tag when restoring from backup.
 */
private UserProfile update(UserProfile dto, ObjectSchema schema, boolean fromBackup) throws DatastoreException, InvalidModelException, NotFoundException, ConflictingUpdateException {
    DBOUserProfile dbo = null;
    MapSqlParameterSource param = new MapSqlParameterSource();
    param.addValue(DBOUserProfile.OWNER_ID_FIELD_NAME, dto.getOwnerId());
    try {
        dbo = simpleJdbcTemplate.queryForObject(SELECT_FOR_UPDATE_SQL, TABLE_MAPPING, param);
    } catch (EmptyResultDataAccessException e) {
        throw new NotFoundException("The resource you are attempting to access cannot be found");
    }
    if (!fromBackup) {
        // if different rollback and throw a meaningful exception
        if (!dbo.geteTag().equals(dto.getEtag())) {
            throw new ConflictingUpdateException("Use profile was updated since you last fetched it, retrieve it again and reapply the update.");
        }
    }
    UserProfileUtils.copyDtoToDbo(dto, dbo, schema);
    if (!fromBackup) {
        // Update with a new e-tag; otherwise, the backup e-tag is used implicitly
        dbo.seteTag(eTagGenerator.generateETag(dbo));
    }
    boolean success = basicDao.update(dbo);
    if (!success)
        throw new DatastoreException("Unsuccessful updating user profile in database.");
    UserProfile resultantDto = new UserProfile();
    UserProfileUtils.copyDboToDto(dbo, resultantDto, schema);
    return resultantDto;
}
Also used : DBOUserProfile(org.sagebionetworks.repo.model.dbo.persistence.DBOUserProfile) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ConflictingUpdateException(org.sagebionetworks.repo.model.ConflictingUpdateException) UserProfile(org.sagebionetworks.repo.model.UserProfile) DBOUserProfile(org.sagebionetworks.repo.model.dbo.persistence.DBOUserProfile) NotFoundException(org.sagebionetworks.repo.web.NotFoundException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) DatastoreException(org.sagebionetworks.repo.model.DatastoreException)

Example 4 with UserProfile

use of org.sagebionetworks.repo.model.UserProfile in project Synapse-Repository-Services by Sage-Bionetworks.

the class UserManagerImpl method getDisplayName.

/**
 * @param principalId
 * @return for a group, returns the group name, for a user returns the display name in the user's profile
 */
@Override
public String getDisplayName(Long principalId) throws NotFoundException, DatastoreException {
    UserGroup userGroup = userGroupDAO.get(principalId.toString());
    if (userGroup.getIsIndividual()) {
        ObjectSchema schema = SchemaCache.getSchema(UserProfile.class);
        UserProfile userProfile = userProfileDAO.get(principalId.toString(), schema);
        return userProfile.getDisplayName();
    } else {
        return userGroup.getName();
    }
}
Also used : ObjectSchema(org.sagebionetworks.schema.ObjectSchema) UserProfile(org.sagebionetworks.repo.model.UserProfile) UserGroup(org.sagebionetworks.repo.model.UserGroup)

Example 5 with UserProfile

use of org.sagebionetworks.repo.model.UserProfile in project Synapse-Repository-Services by Sage-Bionetworks.

the class UserProfileManagerImpl method updateUserProfile.

/**
 * This method is only available to the object owner or an admin
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public UserProfile updateUserProfile(UserInfo userInfo, UserProfile updated) throws NotFoundException, DatastoreException, UnauthorizedException, ConflictingUpdateException, InvalidModelException {
    ObjectSchema schema = SchemaCache.getSchema(UserProfile.class);
    UserProfile userProfile = userProfileDAO.get(updated.getOwnerId(), schema);
    boolean canUpdate = UserProfileManagerUtils.isOwnerOrAdmin(userInfo, userProfile.getOwnerId());
    if (!canUpdate)
        throw new UnauthorizedException("Only owner or administrator may update UserProfile.");
    attachmentManager.checkAttachmentsForPreviews(updated);
    return userProfileDAO.update(updated, schema);
}
Also used : ObjectSchema(org.sagebionetworks.schema.ObjectSchema) UserProfile(org.sagebionetworks.repo.model.UserProfile) UnauthorizedException(org.sagebionetworks.repo.model.UnauthorizedException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserProfile (org.sagebionetworks.repo.model.UserProfile)49 Test (org.junit.Test)25 UserGroup (org.sagebionetworks.repo.model.UserGroup)17 ObjectSchema (org.sagebionetworks.schema.ObjectSchema)13 UserInfo (org.sagebionetworks.repo.model.UserInfo)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)6 DBOUserProfile (org.sagebionetworks.repo.model.dbo.persistence.DBOUserProfile)5 NotFoundException (org.sagebionetworks.repo.web.NotFoundException)5 JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)5 UserSessionData (org.sagebionetworks.repo.model.UserSessionData)4 Date (java.util.Date)3 Before (org.junit.Before)3 SynapseException (org.sagebionetworks.client.exceptions.SynapseException)3 ACCESS_TYPE (org.sagebionetworks.repo.model.ACCESS_TYPE)3 AccessControlList (org.sagebionetworks.repo.model.AccessControlList)3 AccessRequirement (org.sagebionetworks.repo.model.AccessRequirement)3 PrincipalBackup (org.sagebionetworks.repo.model.PrincipalBackup)3 AttachmentData (org.sagebionetworks.repo.model.attachment.AttachmentData)3 FileOutputStream (java.io.FileOutputStream)2