Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. * Copyright 2016 NueveBit, todos los derechos reservados.
  2. */
  3. package com.nuevebit.acec.binder.ws.rest.util;
  4.  
  5. import com.nuevebit.acec.binder.core.auth.Rol;
  6. import com.nuevebit.acec.binder.core.auth.Usuario;
  7. import com.nuevebit.acec.binder.core.auth.UsuarioRepository;
  8. import com.nuevebit.acec.binder.core.util.CRUDService;
  9. import com.nuevebit.persistence.Identificable;
  10. import com.nuevebit.persistence.repository.SearchableRepository;
  11. import javax.inject.Inject;
  12. import org.restlet.data.Status;
  13. import org.restlet.resource.Post;
  14. import org.restlet.resource.ResourceException;
  15.  
  16. /**
  17. *
  18. * @author emerino
  19. */
  20. public abstract class EntitiesResource<T extends Identificable<?>, S>
  21. extends SearchableResource<T, S> {
  22.  
  23. @Inject
  24. private UsuarioRepository usuarioRepository;
  25.  
  26. @Override
  27. protected SearchableRepository<T, ?, S> getRepository()throws ResourceException{
  28. Usuario usuario = getUsuarioAutenticado();
  29.  
  30. if (usuario != null) {
  31. if (!Rol.SUPER_ADMIN.equals(usuario.getRol())) {
  32. throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED);
  33. }
  34. }
  35.  
  36. return getCRUDService();
  37. }
  38.  
  39. protected abstract CRUDService<T, ?, S> getCRUDService();
  40.  
  41. @Post("json")
  42. public T create(T obj) throws Exception {
  43. return getCRUDService().store(obj);
  44. }
  45.  
  46. /**
  47. * Obtener el Usuario autenticado.
  48. *
  49. * @return
  50. */
  51. protected Usuario getUsuarioAutenticado() {
  52. return usuarioRepository
  53. .findByAlias(getChallengeResponse().getIdentifier());
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement