View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.portal.business.role;
35  
36  import fr.paris.lutece.portal.business.user.AdminUser;
37  import fr.paris.lutece.portal.service.spring.SpringContextService;
38  import fr.paris.lutece.portal.service.util.AppPropertiesService;
39  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
40  import fr.paris.lutece.util.ReferenceList;
41  
42  import java.util.Collection;
43  
44  /**
45   * This class provides instances management methods (create, find, ...) for Role right objects
46   */
47  
48  // TODO : change Role management (deletion of role 'none', manage role in mylutece exclusively)
49  public final class RoleHome
50  {
51      // Properties
52      private static final String PROPERTY_DEFAULT_ROLE_CODE = "defaultRole.code";
53      private static final String PROPERTY_DEFAULT_ROLE_DESCRIPTION = "defaultRole.description";
54  
55      // Static variable pointed at the DAO instance
56      private static IRoleDAO _dao = SpringContextService.getBean( "roleDAO" );
57  
58      /**
59       * Creates a new RoleHome object.
60       */
61      private RoleHome( )
62      {
63      }
64  
65      /**
66       * Creation of an instance of a mode
67       *
68       * @param role
69       *            An instance of a role which contains the informations to create
70       * @return The instance of a mode which has been created with its primary key.
71       */
72      public static Roleef="../../../../../../fr/paris/lutece/portal/business/role/Role.html#Role">Role create( Role role )
73      {
74          if ( !role.getRole( ).equals( getDefaultRole( ).getRole( ) ) )
75          {
76              _dao.insert( role );
77          }
78  
79          return role;
80      }
81  
82      /**
83       * Update of the mode which is specified
84       *
85       * @param role
86       *            The instance of the role which contains the data to store
87       * @return The instance of the mode which has been updated
88       */
89      public static Roleef="../../../../../../fr/paris/lutece/portal/business/role/Role.html#Role">Role update( Role role )
90      {
91          if ( !role.getRole( ).equals( getDefaultRole( ).getRole( ) ) )
92          {
93              _dao.store( role );
94          }
95  
96          return role;
97      }
98  
99      /**
100      * Remove the mode whose identifier is specified in parameter
101      *
102      * @param strRole
103      *            The identifier of the role to remove
104      */
105     public static void remove( String strRole )
106     {
107         _dao.delete( strRole );
108     }
109 
110     // /////////////////////////////////////////////////////////////////////////
111     // Finders
112 
113     /**
114      * Returns an instance of an role whose identifier is specified in parameter
115      *
116      * @param strRole
117      *            The mode primary key
118      * @return an instance of a role
119      */
120     public static Role findByPrimaryKey( String strRole )
121     {
122         Role role = getDefaultRole( );
123 
124         if ( !strRole.equals( role.getRole( ) ) )
125         {
126             return _dao.load( strRole );
127         }
128 
129         return role;
130     }
131 
132     /**
133      * Return the list of all roles
134      *
135      * @return A ReferenceList of roles
136      */
137     public static ReferenceList getRolesList( )
138     {
139         ReferenceList roleList = _dao.selectRolesList( );
140         Role defaultRole = getDefaultRole( );
141         roleList.addItem( defaultRole.getRole( ), defaultRole.getRoleDescription( ) );
142 
143         return roleList;
144     }
145 
146     /**
147      * Returns the roles list
148      *
149      * @return Collection of Role
150      */
151     public static Collection<Role> findAll( )
152     {
153         Collection<Role> roleList = _dao.selectAll( );
154         roleList.add( getDefaultRole( ) );
155 
156         return roleList;
157     }
158 
159     /**
160      * Test if the role exists
161      * 
162      * @param strRole
163      *            The role name
164      * @return true if the role exists, otherwise false
165      */
166     public static boolean findExistRole( String strRole )
167     {
168         if ( strRole.equals( getDefaultRole( ).getRole( ) ) )
169         {
170             return true;
171         }
172 
173         return findByPrimaryKey( strRole ) != null;
174     }
175 
176     /**
177      * Return default role
178      *
179      * @return the default role
180      */
181     private static Role getDefaultRole( )
182     {
183         Rolertal/business/role/Role.html#Role">Role role = new Role( );
184         role.setRole( AppPropertiesService.getProperty( PROPERTY_DEFAULT_ROLE_CODE ) );
185         role.setRoleDescription( AppPropertiesService.getProperty( PROPERTY_DEFAULT_ROLE_DESCRIPTION ) );
186 
187         return role;
188     }
189 
190     /**
191      * Return the list of all roles
192      * 
193      * @param user
194      *            The Admin User
195      * @return A ReferenceList of roles
196      */
197     public static ReferenceList getRolesList( AdminUser user )
198     {
199         Collection<Role> listRoles = RoleHome.findAll( );
200         listRoles = AdminWorkgroupService.getAuthorizedCollection( listRoles, user );
201 
202         ReferenceListist.html#ReferenceList">ReferenceList roleList = new ReferenceList( );
203 
204         for ( Role role : listRoles )
205         {
206             roleList.addItem( role.getRole( ), role.getRoleDescription( ) );
207         }
208 
209         return roleList;
210     }
211 }