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.plugins.mylutece.modules.wssodatabase.authentication.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  
42  
43  /**
44   * This class provides Data Access methods for WssoRole objects
45   */
46  public final class WssoUserRoleDAO implements IWssoUserRoleDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_SELECTALL_FOR_USER = " SELECT ur.role FROM mylutece_wsso_user_role ur WHERE ur.mylutece_wsso_user_id = ? ORDER BY ur.role ";
50      private static final String SQL_QUERY_SELECTALL_FOR_ROLE = " SELECT ur.mylutece_wsso_user_id FROM mylutece_wsso_user_role ur WHERE ur.role = ? ORDER BY ur.mylutece_wsso_user_id ";
51      private static final String SQL_QUERY_DELETE_ROLES_FOR_USER = "DELETE FROM mylutece_wsso_user_role WHERE mylutece_wsso_user_id = ?";
52      private static final String SQL_QUERY_INSERT_ROLE_FOR_USER = "INSERT INTO mylutece_wsso_user_role ( mylutece_wsso_user_id, role ) VALUES ( ?, ? ) ";
53  
54      /** This class implements the Singleton design pattern. */
55      private static WssoUserRoleDAO _dao = new WssoUserRoleDAO(  );
56  
57      /**
58       * Creates a new WssoRoleDAO object.
59       */
60      private WssoUserRoleDAO(  )
61      {
62      }
63  
64      /**
65       * Returns the unique instance of the singleton.
66       *
67       * @return the instance
68       */
69      static WssoUserRoleDAO getInstance(  )
70      {
71          return _dao;
72      }
73  
74      /**
75       * Load the list of Roles for a user
76       * @param nIdUser The id of the user
77       * @param plugin The Plugin using this data access service
78       * @return The Collection of the roles key
79       */
80      public Collection<String> selectRoleListForUser( int nIdUser, Plugin plugin )
81      {
82          Collection<String> listRoles = new ArrayList(  );
83          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_FOR_USER, plugin );
84          daoUtil.setInt( 1, nIdUser );
85          daoUtil.executeQuery(  );
86  
87          while ( daoUtil.next(  ) )
88          {
89              listRoles.add( daoUtil.getString( 1 ) );
90          }
91  
92          daoUtil.free(  );
93  
94          return listRoles;
95      }
96  
97      /**
98       * Returns a collection of User id
99       * @param strRoleKey The key of the role
100      * @param plugin The current plugin using this method
101      * @return A collection of User id
102      */
103     public Collection<Integer> selectUserListForRole( String strRoleKey, Plugin plugin )
104     {
105         Collection<Integer> listUserId = new ArrayList<Integer>(  );
106         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_FOR_ROLE, plugin );
107         daoUtil.setString( 1, strRoleKey );
108         daoUtil.executeQuery(  );
109 
110         while ( daoUtil.next(  ) )
111         {
112             listUserId.add( daoUtil.getInt( 1 ) );
113         }
114 
115         daoUtil.free(  );
116 
117         return listUserId;
118     }
119 
120     /**
121      * Delete roles for a user
122      * @param nIdUser The id of the user
123      * @param plugin The Plugin using this data access service
124      */
125     public void deleteRolesForUser( int nIdUser, Plugin plugin )
126     {
127         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ROLES_FOR_USER, plugin );
128         daoUtil.setInt( 1, nIdUser );
129 
130         daoUtil.executeUpdate(  );
131         daoUtil.free(  );
132     }
133 
134     /**
135      * Assign a role to user
136      * @param nIdUser The id of the user
137      * @param strRoleId The role key
138      * @param plugin The Plugin using this data access service
139      */
140     public void createRoleForUser( int nIdUser, String strRoleId, Plugin plugin )
141     {
142         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_ROLE_FOR_USER, plugin );
143         daoUtil.setInt( 1, nIdUser );
144         daoUtil.setString( 2, strRoleId );
145 
146         daoUtil.executeUpdate(  );
147         daoUtil.free(  );
148     }
149 }