ProjectConditions.java

  1. /*
  2.  * The coLAB project
  3.  * Copyright (C) 2021-2023 AlbaSim, MEI, HEIG-VD, HES-SO
  4.  *
  5.  * Licensed under the MIT License
  6.  */
  7. package ch.colabproject.colab.api.security.permissions.project;

  8. import ch.colabproject.colab.api.controller.RequestManager;
  9. import ch.colabproject.colab.api.controller.security.SecurityManager;
  10. import ch.colabproject.colab.api.security.permissions.Conditions.Condition;
  11. import java.util.Objects;

  12. /**
  13.  * Project access control conditions
  14.  *
  15.  * @author sandra
  16.  */
  17. public final class ProjectConditions {

  18.     /**
  19.      * Private constructor prevents instantiation
  20.      */
  21.     private ProjectConditions() {
  22.         throw new UnsupportedOperationException("This is a utility class");
  23.     }

  24.     /**
  25.      * Has the current user access to the project ?
  26.      *
  27.      * @author sandra
  28.      */
  29.     public static class IsProjectReadable extends Condition {

  30.         /** the project id */
  31.         private final Long projectId;

  32.         /**
  33.          * Create a has project read access statement
  34.          *
  35.          * @param projectId the project id
  36.          */
  37.         public IsProjectReadable(Long projectId) {
  38.             this.projectId = projectId;
  39.         }

  40.         @Override
  41.         protected boolean internalEval(RequestManager requestManager,
  42.             SecurityManager securityManager) {
  43.             return securityManager.isProjectReadableByCurrentUser(this.projectId);
  44.         }

  45.         @Override
  46.         public String toString() {
  47.             return "IsProjectReadable(" + projectId + ")";
  48.         }

  49.         @Override
  50.         public int hashCode() {
  51.             int hash = 3;
  52.             hash = 53 * hash + Objects.hashCode(this.projectId);
  53.             return hash;
  54.         }

  55.         @Override
  56.         public boolean equals(Object obj) {
  57.             if (this == obj) {
  58.                 return true;
  59.             }
  60.             if (obj == null) {
  61.                 return false;
  62.             }
  63.             if (getClass() != obj.getClass()) {
  64.                 return false;
  65.             }
  66.             final IsProjectReadable other = (IsProjectReadable) obj;
  67.             if (!Objects.equals(this.projectId, other.projectId)) {
  68.                 return false;
  69.             }
  70.             return true;
  71.         }
  72.     }

  73.     /**
  74.      * Has the current user access to the copy parameters of a project ?
  75.      *
  76.      * @author sandra
  77.      */
  78.     public static class IsCopyParamReadable extends Condition {

  79.         /** the project id */
  80.         private final Long projectId;

  81.         /**
  82.          * Create a has project copy parameters read access statement
  83.          *
  84.          * @param projectId the project id
  85.          */
  86.         public IsCopyParamReadable(Long projectId) {
  87.             this.projectId = projectId;
  88.         }

  89.         @Override
  90.         protected boolean internalEval(RequestManager requestManager,
  91.             SecurityManager securityManager) {
  92.             return securityManager.isCopyParamReadableByCurrentUser(this.projectId);
  93.         }

  94.         @Override
  95.         public String toString() {
  96.             return "IsCopyParamReadable(" + projectId + ")";
  97.         }

  98.         @Override
  99.         public int hashCode() {
  100.             int hash = 3;
  101.             hash = 53 * hash + Objects.hashCode(this.projectId);
  102.             return hash;
  103.         }

  104.         @Override
  105.         public boolean equals(Object obj) {
  106.             if (this == obj) {
  107.                 return true;
  108.             }
  109.             if (obj == null) {
  110.                 return false;
  111.             }
  112.             if (getClass() != obj.getClass()) {
  113.                 return false;
  114.             }
  115.             final IsCopyParamReadable other = (IsCopyParamReadable) obj;
  116.             if (!Objects.equals(this.projectId, other.projectId)) {
  117.                 return false;
  118.             }
  119.             return true;
  120.         }
  121.     }

  122. }