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