more generalized controller endpoints

This commit is contained in:
anhefti 2019-01-20 21:05:33 +01:00
parent ca20785400
commit bf45576610
20 changed files with 197 additions and 283 deletions

View file

@ -12,9 +12,17 @@ public interface Entity extends ModelIdAware {
public static final String ATTR_ID = "id";
public static final String ATTR_INSTITUTION = "institution";
public static final String ATTR_ACTIVE = "active";
EntityType entityType();
String getName();
public static EntityKeyAndName toName(final Entity entity) {
return new EntityKeyAndName(
entity.entityType(),
entity.getModelId(),
entity.getName());
}
}

View file

@ -17,7 +17,6 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import ch.ethz.seb.sebserver.gbl.model.Activatable;
import ch.ethz.seb.sebserver.gbl.model.Domain;
import ch.ethz.seb.sebserver.gbl.model.Domain.INSTITUTION;
import ch.ethz.seb.sebserver.gbl.model.EntityKeyAndName;
import ch.ethz.seb.sebserver.gbl.model.EntityType;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.GrantEntity;
@ -110,11 +109,4 @@ public final class Institution implements GrantEntity, Activatable {
+ ", active=" + this.active + "]";
}
public static EntityKeyAndName toName(final Institution institution) {
return new EntityKeyAndName(
EntityType.INSTITUTION,
String.valueOf(institution.id),
institution.name);
}
}

View file

@ -20,7 +20,6 @@ import org.springframework.transaction.annotation.Transactional;
import ch.ethz.seb.sebserver.gbl.model.Entity;
import ch.ethz.seb.sebserver.gbl.model.EntityKey;
import ch.ethz.seb.sebserver.gbl.model.EntityKeyAndName;
import ch.ethz.seb.sebserver.gbl.model.EntityType;
import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.ActivatableEntityDAO;
@ -37,22 +36,6 @@ public interface BulkActionSupportDAO<T extends Entity> {
Set<EntityKey> getDependencies(BulkAction bulkAction);
Result<Collection<T>> bulkLoadEntities(Collection<EntityKey> keys);
@Transactional(readOnly = true)
default Result<Collection<EntityKeyAndName>> bulkLoadEntityNames(final Collection<EntityKey> keys) {
return Result.tryCatch(() -> {
return bulkLoadEntities(keys)
.getOrThrow()
.stream()
.map(entity -> new EntityKeyAndName(
EntityType.INSTITUTION,
entity.getModelId(),
entity.getName()))
.collect(Collectors.toList());
});
}
@Transactional
default Collection<Result<EntityKey>> processBulkAction(final BulkAction bulkAction) {
final Set<EntityKey> all = bulkAction.extractKeys(entityType());
@ -60,15 +43,15 @@ public interface BulkActionSupportDAO<T extends Entity> {
switch (bulkAction.type) {
case ACTIVATE:
return (this instanceof ActivatableEntityDAO)
? ((ActivatableEntityDAO<?>) this).setActive(all, true)
? ((ActivatableEntityDAO<?, ?>) this).setActive(all, true)
: Collections.emptyList();
case DEACTIVATE:
return (this instanceof ActivatableEntityDAO)
? ((ActivatableEntityDAO<?>) this).setActive(all, false)
? ((ActivatableEntityDAO<?, ?>) this).setActive(all, false)
: Collections.emptyList();
case HARD_DELETE:
return (this instanceof EntityDAO)
? ((EntityDAO<?>) this).delete(all)
? ((EntityDAO<?, ?>) this).delete(all)
: Collections.emptyList();
}

View file

@ -15,12 +15,13 @@ import org.springframework.transaction.annotation.Transactional;
import ch.ethz.seb.sebserver.gbl.model.Entity;
import ch.ethz.seb.sebserver.gbl.model.EntityKey;
import ch.ethz.seb.sebserver.gbl.model.ModelIdAware;
import ch.ethz.seb.sebserver.gbl.util.Result;
/** Interface of a DAO for an Entity that has activation feature.
*
* @param <T> the concrete Entity type */
public interface ActivatableEntityDAO<T extends Entity> extends EntityDAO<T> {
public interface ActivatableEntityDAO<T extends Entity, M extends ModelIdAware> extends EntityDAO<T, M> {
/** Get a Collection of all active Entity instances for a concrete entity-domain.
*

View file

@ -15,12 +15,16 @@ import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.springframework.transaction.annotation.Transactional;
import ch.ethz.seb.sebserver.gbl.model.Entity;
import ch.ethz.seb.sebserver.gbl.model.EntityKey;
import ch.ethz.seb.sebserver.gbl.model.EntityKeyAndName;
import ch.ethz.seb.sebserver.gbl.model.EntityType;
import ch.ethz.seb.sebserver.gbl.model.ModelIdAware;
import ch.ethz.seb.sebserver.gbl.util.Result;
public interface EntityDAO<T extends Entity> {
public interface EntityDAO<T extends Entity, M extends ModelIdAware> {
/** Get the entity type for a concrete EntityDAO implementation.
*
@ -80,6 +84,32 @@ public interface EntityDAO<T extends Entity> {
return all(entity -> true);
}
Result<Collection<T>> loadEntities(Collection<EntityKey> keys);
@Transactional(readOnly = true)
default Result<Collection<EntityKeyAndName>> loadEntityNames(final Collection<EntityKey> keys) {
return Result.tryCatch(() -> {
return loadEntities(keys)
.getOrThrow()
.stream()
.map(entity -> new EntityKeyAndName(
entity.entityType(),
entity.getModelId(),
entity.getName()))
.collect(Collectors.toList());
});
}
/** Use this to save/modify an entity.
* If the model identifier from given modified entity data is null or not exists already, a new entity is created.
* If the model identifier is available and matches an existing entity, all entity data that are
* not null on modified entity data instance are updated within the existing entity.
*
* @param modified modified data instance containing all data that should be modified
* @return A Result of the entity instance where the successfully saved/modified entity data is available or a
* reported exception on error case */
Result<T> save(M modified);
/** Use this to delete a set Entity by a Collection of EntityKey
*
* @param all The Collection of EntityKey to delete

View file

@ -17,7 +17,7 @@ import ch.ethz.seb.sebserver.gbl.model.exam.QuizData;
import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionSupportDAO;
public interface ExamDAO extends ActivatableEntityDAO<Exam>, BulkActionSupportDAO<Exam> {
public interface ExamDAO extends ActivatableEntityDAO<Exam, Exam>, BulkActionSupportDAO<Exam> {
Result<Exam> importFromQuizData(QuizData quizData);
@ -33,6 +33,4 @@ public interface ExamDAO extends ActivatableEntityDAO<Exam>, BulkActionSupportDA
String owner,
Boolean active);
Result<Exam> save(Exam exam);
}

View file

@ -14,10 +14,9 @@ import ch.ethz.seb.sebserver.gbl.model.institution.Institution;
import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionSupportDAO;
public interface InstitutionDAO extends ActivatableEntityDAO<Institution>, BulkActionSupportDAO<Institution> {
public interface InstitutionDAO
extends ActivatableEntityDAO<Institution, Institution>, BulkActionSupportDAO<Institution> {
Result<Collection<Institution>> allMatching(String name, Boolean active);
Result<Institution> save(Institution institution);
}

View file

@ -17,7 +17,7 @@ import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup.LmsType;
import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionSupportDAO;
public interface LmsSetupDAO extends ActivatableEntityDAO<LmsSetup>, BulkActionSupportDAO<LmsSetup> {
public interface LmsSetupDAO extends ActivatableEntityDAO<LmsSetup, LmsSetup>, BulkActionSupportDAO<LmsSetup> {
@Transactional(readOnly = true)
default Result<Collection<LmsSetup>> allOfInstitution(final Long institutionId, final Boolean active) {
@ -26,6 +26,4 @@ public interface LmsSetupDAO extends ActivatableEntityDAO<LmsSetup>, BulkActionS
Result<Collection<LmsSetup>> allMatching(Long institutionId, String name, LmsType lmsType, Boolean active);
Result<LmsSetup> save(LmsSetup lmsSetup);
}

View file

@ -18,7 +18,8 @@ import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.datalayer.batis.model.UserActivityLogRecord;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.SEBServerUser;
public interface UserActivityLogDAO extends UserRelatedEntityDAO<UserActivityLog> {
public interface UserActivityLogDAO
extends EntityDAO<UserActivityLog, UserActivityLog>, UserRelatedEntityDAO<UserActivityLog> {
enum ActivityType {
CREATE,

View file

@ -24,7 +24,7 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionSuppor
/** The Data Access Object for all User related data like get user data within UserInfo,
* save and modify user related data within UserMod and get internal user principal data
* within SEBServerUser. */
public interface UserDAO extends ActivatableEntityDAO<UserInfo>, BulkActionSupportDAO<UserInfo> {
public interface UserDAO extends ActivatableEntityDAO<UserInfo, UserMod>, BulkActionSupportDAO<UserInfo> {
/** Use this to get the user id (PK) from a given UUID.
*
@ -68,16 +68,6 @@ public interface UserDAO extends ActivatableEntityDAO<UserInfo>, BulkActionSuppo
* @return a Result of Collection of filtered UserInfo. Or an exception result on error case */
Result<Collection<UserInfo>> all(UserFilter filter, Predicate<UserInfo> predicate);
/** Use this to save/modify user data.
* If the UUID from given UserMod is null or not exists already, a new user is created.
* If the UUID is available and matches an existing user record, all user data that are
* not null on UserMod instance are updated within the existing user record.
*
* @param userMod UserMod instance containing new user record data
* @return A Result of UserInfo where the successfully saved/modified user data is available or a reported
* exception on error case */
Result<UserInfo> save(UserMod userMod);
/** Use this to get a Collection containing EntityKey's of all entities that belongs to a given User.
*
* @param uuid The UUID of the user

View file

@ -17,7 +17,7 @@ import ch.ethz.seb.sebserver.gbl.util.Result;
*
*
* @param <T> the concrete type of the Entity */
public interface UserRelatedEntityDAO<T extends Entity> extends EntityDAO<T> {
public interface UserRelatedEntityDAO<T extends Entity> {
/** Get all Entity instances that has a relation to the user-account
* of a given user identity (UUID)

View file

@ -264,7 +264,7 @@ public class ExamDAOImpl implements ExamDAO {
@Override
@Transactional(readOnly = true)
public Result<Collection<Exam>> bulkLoadEntities(final Collection<EntityKey> keys) {
public Result<Collection<Exam>> loadEntities(final Collection<EntityKey> keys) {
return Result.tryCatch(() -> {
final List<Long> ids = extractPKsFromKeys(keys);
return this.examRecordMapper.selectByExample()

View file

@ -175,7 +175,7 @@ public class InstitutionDAOImpl implements InstitutionDAO {
@Override
@Transactional(readOnly = true)
public Result<Collection<Institution>> bulkLoadEntities(final Collection<EntityKey> keys) {
public Result<Collection<Institution>> loadEntities(final Collection<EntityKey> keys) {
return Result.tryCatch(() -> {
final List<Long> ids = extractPKsFromKeys(keys);

View file

@ -194,7 +194,7 @@ public class LmsSetupDAOImpl implements LmsSetupDAO {
@Override
@Transactional(readOnly = true)
public Result<Collection<LmsSetup>> bulkLoadEntities(final Collection<EntityKey> keys) {
public Result<Collection<LmsSetup>> loadEntities(final Collection<EntityKey> keys) {
return Result.tryCatch(() -> {
final List<Long> ids = extractPKsFromKeys(keys);

View file

@ -277,6 +277,20 @@ public class UserActivityLogDAOImpl implements UserActivityLogDAO {
@Override
@Transactional(readOnly = true)
public Result<Collection<UserActivityLog>> loadEntities(final Collection<EntityKey> keys) {
// TODO Auto-generated method stub
return null;
}
@Override
@Transactional
public Result<UserActivityLog> save(final UserActivityLog modified) {
// TODO Auto-generated method stub
return null;
}
@Override
@Transactional
public Result<Integer> overwriteUserReferences(final String userUuid, final boolean deactivate) {
return Result.tryCatch(() -> {
final List<UserActivityLogRecord> records = this.userLogRecordMapper.selectByExample()

View file

@ -257,7 +257,7 @@ public class UserDaoImpl implements UserDAO {
@Override
@Transactional(readOnly = true)
public Result<Collection<UserInfo>> bulkLoadEntities(final Collection<EntityKey> keys) {
public Result<Collection<UserInfo>> loadEntities(final Collection<EntityKey> keys) {
return Result.tryCatch(() -> {
final List<Long> ids = extractPKsFromKeys(keys);

View file

@ -16,6 +16,7 @@ import ch.ethz.seb.sebserver.gbl.model.EntityKey;
import ch.ethz.seb.sebserver.gbl.model.EntityProcessingReport;
import ch.ethz.seb.sebserver.gbl.model.EntityType;
import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.servicelayer.PaginationService;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.AuthorizationGrantService;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.GrantEntity;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.PrivilegeType;
@ -23,15 +24,19 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkAction;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkAction.Type;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionService;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.EntityDAO;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO;
public abstract class ActivatableEntityController<T extends GrantEntity> extends EntityController<T> {
public abstract class ActivatableEntityController<T extends GrantEntity, M extends GrantEntity>
extends EntityController<T, M> {
public ActivatableEntityController(
final AuthorizationGrantService authorizationGrantService,
final BulkActionService bulkActionService,
final EntityDAO<T> entityDAO) {
final EntityDAO<T, M> entityDAO,
final UserActivityLogDAO userActivityLogDAO,
final PaginationService paginationService) {
super(authorizationGrantService, bulkActionService, entityDAO);
super(authorizationGrantService, bulkActionService, entityDAO, userActivityLogDAO, paginationService);
}
@RequestMapping(path = "/{id}/activate", method = RequestMethod.POST)

View file

@ -8,39 +8,64 @@
package ch.ethz.seb.sebserver.webservice.weblayer.api;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import javax.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import ch.ethz.seb.sebserver.gbl.Constants;
import ch.ethz.seb.sebserver.gbl.model.Entity;
import ch.ethz.seb.sebserver.gbl.model.EntityKey;
import ch.ethz.seb.sebserver.gbl.model.EntityKeyAndName;
import ch.ethz.seb.sebserver.gbl.model.EntityProcessingReport;
import ch.ethz.seb.sebserver.gbl.model.EntityType;
import ch.ethz.seb.sebserver.gbl.model.Page;
import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.UserRecordDynamicSqlSupport;
import ch.ethz.seb.sebserver.webservice.servicelayer.PaginationService;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.AuthorizationGrantService;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.GrantEntity;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.PrivilegeType;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.UserService;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkAction;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkAction.Type;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionService;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.EntityDAO;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO.ActivityType;
public abstract class EntityController<T extends GrantEntity> {
public abstract class EntityController<T extends GrantEntity, M extends GrantEntity> {
protected final AuthorizationGrantService authorizationGrantService;
protected final BulkActionService bulkActionService;
protected final EntityDAO<T> entityDAO;
protected final EntityDAO<T, M> entityDAO;
protected final UserActivityLogDAO userActivityLogDAO;
protected final PaginationService paginationService;
protected EntityController(
final AuthorizationGrantService authorizationGrantService,
final BulkActionService bulkActionService,
final EntityDAO<T> entityDAO) {
final EntityDAO<T, M> entityDAO,
final UserActivityLogDAO userActivityLogDAO,
final PaginationService paginationService) {
this.authorizationGrantService = authorizationGrantService;
this.bulkActionService = bulkActionService;
this.entityDAO = entityDAO;
this.userActivityLogDAO = userActivityLogDAO;
this.paginationService = paginationService;
}
@RequestMapping(path = "/{id}", method = RequestMethod.GET)
public T accountInfo(@PathVariable final String id) {
public T byId(@PathVariable final String id) {
return this.entityDAO
.byModelId(id)
.flatMap(entity -> this.authorizationGrantService.checkGrantOnEntity(
@ -49,6 +74,58 @@ public abstract class EntityController<T extends GrantEntity> {
.getOrThrow();
}
@RequestMapping(path = "/all", method = RequestMethod.GET)
public Page<T> allActive(
@RequestParam(
name = Entity.ATTR_INSTITUTION,
required = true,
defaultValue = UserService.USERS_INSTITUTION_AS_DEFAULT) final Long institutionId,
@RequestParam(name = Page.ATTR_PAGE_NUMBER, required = false) final Integer pageNumber,
@RequestParam(name = Page.ATTR_PAGE_SIZE, required = false) final Integer pageSize,
@RequestParam(name = Page.ATTR_SORT_BY, required = false) final String sortBy,
@RequestParam(name = Page.ATTR_SORT_ORDER, required = false) final Page.SortOrder sortOrder) {
checkReadPrivilege(institutionId);
return this.paginationService.getPage(
pageNumber,
pageSize,
sortBy,
sortOrder,
UserRecordDynamicSqlSupport.userRecord,
() -> this.entityDAO.all(entity -> true, true).getOrThrow());
}
@RequestMapping(path = "/all/in", method = RequestMethod.GET)
public Collection<T> getForIds(
@RequestParam(
name = Entity.ATTR_INSTITUTION,
required = true,
defaultValue = UserService.USERS_INSTITUTION_AS_DEFAULT) final Long institutionId,
@RequestParam(name = "ids", required = true) final String ids) {
checkReadPrivilege(institutionId);
return Result.tryCatch(() -> {
return Arrays.asList(StringUtils.split(ids, Constants.LIST_SEPARATOR_CHAR))
.stream()
.map(modelId -> new EntityKey(modelId, this.entityDAO.entityType()))
.collect(Collectors.toList());
})
.flatMap(this.entityDAO::loadEntities)
.getOrThrow();
}
@RequestMapping(path = "/names", method = RequestMethod.GET)
public Collection<EntityKeyAndName> getNames(
@RequestParam(name = Entity.ATTR_ACTIVE, required = false) final Boolean active) {
return this.entityDAO.all(entity -> true, true)
.getOrThrow()
.stream()
.map(Entity::toName)
.collect(Collectors.toList());
}
@RequestMapping(path = "/{id}/hard-delete", method = RequestMethod.DELETE)
public EntityProcessingReport hardDeleteUser(@PathVariable final String id) {
final EntityType entityType = this.entityDAO.entityType();
@ -65,4 +142,32 @@ public abstract class EntityController<T extends GrantEntity> {
.getOrThrow();
}
@RequestMapping(path = "/create", method = RequestMethod.PUT)
public T create(@Valid @RequestBody final M modifyData) {
return this.authorizationGrantService.checkGrantOnEntity(modifyData, PrivilegeType.WRITE)
.flatMap(entity -> this.entityDAO.save(modifyData))
.flatMap(entity -> this.userActivityLogDAO.log(ActivityType.CREATE, entity))
.flatMap(entity -> notifySave(modifyData, entity))
.getOrThrow();
}
@RequestMapping(path = "/save", method = RequestMethod.POST)
public T save(@Valid @RequestBody final M modifyData) {
return this.authorizationGrantService.checkGrantOnEntity(modifyData, PrivilegeType.MODIFY)
.flatMap(entity -> this.entityDAO.save(modifyData))
.flatMap(entity -> this.userActivityLogDAO.log(ActivityType.MODIFY, entity))
.flatMap(entity -> notifySave(modifyData, entity))
.getOrThrow();
}
protected Result<T> notifySave(final M modifyData, final T entity) {
return Result.of(entity);
}
protected void checkReadPrivilege(final Long institutionId) {
this.authorizationGrantService.checkPrivilege(
this.entityDAO.entityType(),
PrivilegeType.READ_ONLY,
institutionId);
}
}

View file

@ -10,54 +10,42 @@ package ch.ethz.seb.sebserver.webservice.weblayer.api;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ch.ethz.seb.sebserver.gbl.model.EntityKey;
import ch.ethz.seb.sebserver.gbl.model.EntityKeyAndName;
import ch.ethz.seb.sebserver.gbl.model.EntityProcessingReport;
import ch.ethz.seb.sebserver.gbl.model.EntityType;
import ch.ethz.seb.sebserver.gbl.model.institution.Institution;
import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile;
import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.servicelayer.PaginationService;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.AuthorizationGrantService;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.PrivilegeType;
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.SEBServerUser;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkAction;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkAction.Type;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionService;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.InstitutionDAO;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO.ActivityType;
@WebServiceProfile
@RestController
@RequestMapping("/${sebserver.webservice.api.admin.endpoint}" + RestAPI.ENDPOINT_INSTITUTION)
public class InstitutionController {
public class InstitutionController extends ActivatableEntityController<Institution, Institution> {
private final InstitutionDAO institutionDAO;
private final AuthorizationGrantService authorizationGrantService;
private final UserActivityLogDAO userActivityLogDAO;
private final BulkActionService bulkActionService;
public InstitutionController(
final InstitutionDAO institutionDAO,
final AuthorizationGrantService authorizationGrantService,
final UserActivityLogDAO userActivityLogDAO,
final BulkActionService bulkActionService) {
final BulkActionService bulkActionService,
final PaginationService paginationService) {
super(authorizationGrantService, bulkActionService, institutionDAO, userActivityLogDAO, paginationService);
this.institutionDAO = institutionDAO;
this.authorizationGrantService = authorizationGrantService;
this.userActivityLogDAO = userActivityLogDAO;
this.bulkActionService = bulkActionService;
}
@RequestMapping(path = "/self", method = RequestMethod.GET)
@ -70,16 +58,6 @@ public class InstitutionController {
return this.institutionDAO.byPK(institutionId).getOrThrow();
}
@RequestMapping(path = "/{id}", method = RequestMethod.GET)
public Institution getById(@PathVariable final Long id) {
return this.institutionDAO
.byPK(id)
.flatMap(inst -> this.authorizationGrantService.checkGrantOnEntity(
inst,
PrivilegeType.READ_ONLY))
.getOrThrow();
}
@RequestMapping(method = RequestMethod.GET)
public Collection<Institution> getAll(
@RequestParam(name = Institution.FILTER_ATTR_ACTIVE, required = false) final Boolean active) {
@ -95,94 +73,4 @@ public class InstitutionController {
}
}
@RequestMapping(path = "/names", method = RequestMethod.GET)
public Collection<EntityKeyAndName> getNames(
@RequestParam(name = Institution.FILTER_ATTR_ACTIVE, required = false) final Boolean active) {
return getAll(active)
.stream()
.map(Institution::toName)
.collect(Collectors.toList());
}
@RequestMapping(path = "/create", method = RequestMethod.PUT)
public Institution create(@Valid @RequestBody final Institution institution) {
return save(institution, PrivilegeType.WRITE)
.getOrThrow();
}
@RequestMapping(path = "/save", method = RequestMethod.POST)
public Institution save(@Valid @RequestBody final Institution institution) {
return save(institution, PrivilegeType.MODIFY)
.getOrThrow();
}
@RequestMapping(path = "/{id}/activate", method = RequestMethod.POST)
public EntityProcessingReport activate(@PathVariable final Long id) {
return setActive(id, true)
.getOrThrow();
}
@RequestMapping(value = "/{id}/deactivate", method = RequestMethod.POST)
public EntityProcessingReport deactivate(@PathVariable final Long id) {
return setActive(id, false)
.getOrThrow();
}
@RequestMapping(path = "/{id}/delete", method = RequestMethod.DELETE)
public EntityProcessingReport deleteUser(@PathVariable final Long id) {
checkPrivilegeForInstitution(id, PrivilegeType.WRITE);
return this.bulkActionService.createReport(new BulkAction(
Type.DEACTIVATE,
EntityType.INSTITUTION,
new EntityKey(id, EntityType.INSTITUTION)))
.getOrThrow();
}
@RequestMapping(path = "/{id}/hard-delete", method = RequestMethod.DELETE)
public EntityProcessingReport hardDeleteUser(@PathVariable final Long id) {
checkPrivilegeForInstitution(id, PrivilegeType.WRITE);
return this.bulkActionService.createReport(new BulkAction(
Type.HARD_DELETE,
EntityType.INSTITUTION,
new EntityKey(id, EntityType.INSTITUTION)))
.getOrThrow();
}
private void checkPrivilegeForInstitution(final Long id, final PrivilegeType type) {
this.authorizationGrantService.checkHasAnyPrivilege(
EntityType.INSTITUTION,
type);
this.institutionDAO.byPK(id)
.flatMap(institution -> this.authorizationGrantService.checkGrantOnEntity(
institution,
type))
.getOrThrow();
}
private Result<EntityProcessingReport> setActive(final Long id, final boolean active) {
checkPrivilegeForInstitution(id, PrivilegeType.MODIFY);
return this.bulkActionService.createReport(new BulkAction(
(active) ? Type.ACTIVATE : Type.DEACTIVATE,
EntityType.INSTITUTION,
new EntityKey(id, EntityType.INSTITUTION)));
}
private Result<Institution> save(final Institution institution, final PrivilegeType privilegeType) {
final ActivityType activityType = (institution.id == null)
? ActivityType.CREATE
: ActivityType.MODIFY;
return this.authorizationGrantService
.checkGrantOnEntity(institution, privilegeType)
.flatMap(this.institutionDAO::save)
.flatMap(inst -> this.userActivityLogDAO.log(activityType, inst));
}
}

View file

@ -10,12 +10,9 @@ package ch.ethz.seb.sebserver.webservice.weblayer.api;
import java.util.Collection;
import javax.validation.Valid;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@ -35,20 +32,17 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.PrivilegeType
import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.UserService;
import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionService;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO.ActivityType;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserDAO;
import ch.ethz.seb.sebserver.webservice.weblayer.oauth.RevokeTokenEndpoint;
@WebServiceProfile
@RestController
@RequestMapping("/${sebserver.webservice.api.admin.endpoint}" + RestAPI.ENDPOINT_USER_ACCOUNT)
public class UserAccountController extends ActivatableEntityController<UserInfo> {
public class UserAccountController extends ActivatableEntityController<UserInfo, UserMod> {
private final UserDAO userDao;
private final AuthorizationGrantService authorizationGrantService;
private final UserActivityLogDAO userActivityLogDAO;
private final PaginationService paginationService;
private final BulkActionService bulkActionService;
private final ApplicationEventPublisher applicationEventPublisher;
public UserAccountController(
@ -59,12 +53,10 @@ public class UserAccountController extends ActivatableEntityController<UserInfo>
final BulkActionService bulkActionService,
final ApplicationEventPublisher applicationEventPublisher) {
super(authorizationGrantService, bulkActionService, userDao);
super(authorizationGrantService, bulkActionService, userDao, userActivityLogDAO, paginationService);
this.userDao = userDao;
this.authorizationGrantService = authorizationGrantService;
this.userActivityLogDAO = userActivityLogDAO;
this.paginationService = paginationService;
this.bulkActionService = bulkActionService;
this.applicationEventPublisher = applicationEventPublisher;
}
@ -129,91 +121,8 @@ public class UserAccountController extends ActivatableEntityController<UserInfo>
.getUserInfo();
}
// @Override
// @RequestMapping(path = "/{uuid}", method = RequestMethod.GET)
// public UserInfo accountInfo(@PathVariable final String uuid) {
// return this.userDao
// .byModelId(uuid)
// .flatMap(userInfo -> this.authorizationGrantService.checkGrantOnEntity(
// userInfo,
// PrivilegeType.READ_ONLY))
// .getOrThrow();
// }
@RequestMapping(path = "/create", method = RequestMethod.PUT)
public UserInfo createUser(@Valid @RequestBody final UserMod userData) {
return _saveUser(userData, PrivilegeType.WRITE)
.getOrThrow();
}
@RequestMapping(path = "/save", method = RequestMethod.POST)
public UserInfo saveUser(@Valid @RequestBody final UserMod userData) {
return _saveUser(userData, PrivilegeType.MODIFY)
.getOrThrow();
}
// @Override
// @RequestMapping(path = "/{uuid}/activate", method = RequestMethod.POST)
// public EntityProcessingReport activateUser(@PathVariable final String uuid) {
// return setActive(uuid, true)
// .getOrThrow();
// }
//
// @Override
// @RequestMapping(value = "/{uuid}/deactivate", method = RequestMethod.POST)
// public EntityProcessingReport deactivateUser(@PathVariable final String uuid) {
// return setActive(uuid, false)
// .getOrThrow();
// }
//
// @Override
// @RequestMapping(path = "/{uuid}/delete", method = RequestMethod.DELETE)
// public EntityProcessingReport deleteUser(@PathVariable final String uuid) {
// checkPrivilegeForUser(uuid, PrivilegeType.WRITE);
//
// return this.bulkActionService.createReport(new BulkAction(
// Type.DEACTIVATE,
// EntityType.USER,
// new EntityKey(uuid, EntityType.USER, false)))
// .getOrThrow();
// }
// private void checkPrivilegeForUser(final String uuid, final PrivilegeType type) {
// this.authorizationGrantService.checkHasAnyPrivilege(
// EntityType.USER,
// type);
//
// this.userDao.byModelId(uuid)
// .flatMap(userInfo -> this.authorizationGrantService.checkGrantOnEntity(
// userInfo,
// type))
// .getOrThrow();
// }
// private Result<EntityProcessingReport> setActive(final String uuid, final boolean active) {
// this.checkPrivilegeForUser(uuid, PrivilegeType.MODIFY);
//
// return this.bulkActionService.createReport(new BulkAction(
// (active) ? Type.ACTIVATE : Type.DEACTIVATE,
// EntityType.USER,
// new EntityKey(uuid, EntityType.USER, false)));
// }
private Result<UserInfo> _saveUser(final UserMod userData, final PrivilegeType privilegeType) {
final ActivityType activityType = (userData.uuid == null)
? ActivityType.CREATE
: ActivityType.MODIFY;
return this.authorizationGrantService
.checkGrantOnEntity(userData, privilegeType)
.flatMap(this.userDao::save)
.flatMap(userInfo -> this.userActivityLogDAO.log(activityType, userInfo))
.flatMap(userInfo -> revokePassword(userData, userInfo));
}
private Result<UserInfo> revokePassword(final UserMod userData, final UserInfo userInfo) {
@Override
protected Result<UserInfo> notifySave(final UserMod userData, final UserInfo userInfo) {
// handle password change; revoke access tokens if password has changed
if (userData.passwordChangeRequest() && userData.newPasswordMatch()) {
this.applicationEventPublisher.publishEvent(
@ -256,11 +165,4 @@ public class UserAccountController extends ActivatableEntityController<UserInfo>
: null;
}
private void checkReadPrivilege(final Long institutionId) {
this.authorizationGrantService.checkPrivilege(
EntityType.USER,
PrivilegeType.READ_ONLY,
institutionId);
}
}