View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.releaser.service;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  
40  import fr.paris.lutece.plugins.releaser.business.Cluster;
41  import fr.paris.lutece.plugins.releaser.business.ClusterHome;
42  import fr.paris.lutece.plugins.releaser.business.Site;
43  import fr.paris.lutece.plugins.releaser.business.SiteHome;
44  import fr.paris.lutece.portal.business.user.AdminUser;
45  import fr.paris.lutece.portal.service.rbac.RBACService;
46  
47  /**
48   * ClusterService.
49   */
50  public class ClusterService
51  {
52  
53      /**
54       * Load the list of sites into each cluster object and returns the list of clusters
55       * 
56       * @return the list which contains the data of all the cluster objects
57       */
58      public static List<Cluster> getClustersListWithSites( AdminUser adminUser )
59      {
60          List<Cluster> listCluster = ClusterHome.getClustersList( );
61  
62          for ( Cluster cluster : listCluster )
63          {
64              List<Site> listSite = SiteHome.findByCluster( cluster.getId( ) );
65              for ( Site site : listSite )
66              {
67                  cluster.getSites( ).add( site );
68              }
69          }
70  
71          return listCluster;
72      }
73  
74      /**
75       * Load the list of sites into each cluster object and returns the list of clusters
76       * 
77       * @return the list which contains the data of all the cluster objects
78       */
79      public static List<Cluster> getUserClusters( AdminUser adminUser )
80      {
81          List<Cluster> listCluster = ClusterHome.getClustersList( );
82          List<Cluster> listAuthorizedClusters = new ArrayList<Cluster>( );
83  
84          for ( Cluster cluster : listCluster )
85          {
86              HashMap<String, Boolean> clusterPermissions = new HashMap<String, Boolean>( );
87              boolean bAuthoriseViewCluster = false;
88  
89              // Add site to the cluster permission
90              if ( RBACService.isAuthorized( Cluster.RESOURCE_TYPE, cluster.getResourceId( ), ClusterResourceIdService.PERMISSION_ADD_SITE_TO_CLUSTER,
91                      adminUser ) )
92              {
93                  clusterPermissions.put( Cluster.PERMISSION_ADD_SITES_TO_CLUSTER, true );
94                  bAuthoriseViewCluster = true;
95              }
96              else
97              {
98                  clusterPermissions.put( Cluster.PERMISSION_ADD_SITES_TO_CLUSTER, false );
99              }
100 
101             // Modify cluster permission
102             if ( RBACService.isAuthorized( Cluster.RESOURCE_TYPE, cluster.getResourceId( ), ClusterResourceIdService.PERMISSION_MODIFY, adminUser ) )
103             {
104                 clusterPermissions.put( Cluster.PERMISSION_MODIFY_CLUSTER, true );
105                 bAuthoriseViewCluster = true;
106             }
107             else
108             {
109                 clusterPermissions.put( Cluster.PERMISSION_MODIFY_CLUSTER, false );
110             }
111 
112             // Delete cluster permission
113             if ( RBACService.isAuthorized( Cluster.RESOURCE_TYPE, cluster.getResourceId( ), ClusterResourceIdService.PERMISSION_DELETE, adminUser ) )
114             {
115                 clusterPermissions.put( Cluster.PERMISSION_DELETE_CLUSTER, true );
116                 bAuthoriseViewCluster = true;
117             }
118             else
119             {
120                 clusterPermissions.put( Cluster.PERMISSION_DELETE_CLUSTER, false );
121             }
122 
123             // Add permissions to the cluster
124             cluster.setPermissions( clusterPermissions );
125 
126             // Add autorized sites
127             List<Site> listAuthorizedSites = SiteService.getAuthorizedSites( cluster.getId( ), adminUser );
128 
129             if ( listAuthorizedSites != null )
130             {
131                 cluster.setSites( listAuthorizedSites );
132 
133                 if ( !listAuthorizedSites.isEmpty( ) )
134                 {
135                     bAuthoriseViewCluster = true;
136                 }
137             }
138 
139             if ( bAuthoriseViewCluster )
140                 listAuthorizedClusters.add( cluster );
141         }
142 
143         return listAuthorizedClusters;
144     }
145 
146     public static boolean IsAddClusterAuthorized( AdminUser adminUser )
147     {
148 
149         if ( RBACService.isAuthorized( new Cluster( ), ClusterResourceIdService.PERMISSION_ADD, adminUser ) )
150         {
151             return true;
152         }
153 
154         return false;
155     }
156 
157     public static boolean IsUserAuthorized( AdminUser adminUser, String clusterId, String permission )
158     {
159 
160         boolean bAuthorized = false;
161 
162         if ( RBACService.isAuthorized( Cluster.RESOURCE_TYPE, clusterId, permission, adminUser ) )
163         {
164             bAuthorized = true;
165         }
166 
167         return bAuthorized;
168     }
169 }