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.mylutece.modules.database.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.List;
41  
42  /**
43   * This class provides Data Access methods for Group objects
44   */
45  public class GroupRoleDAO implements IGroupRoleDAO
46  {
47      public static final String SQL_QUERY_FIND_ROLES_FROM_GROUP_ID = "SELECT role_key FROM mylutece_database_group_role WHERE group_key like ? ";
48      public static final String SQL_QUERY_FIND_GROUPS_FROM_ROLE_ID = "SELECT group_key FROM mylutece_database_group_role WHERE role_key = ? ";
49      private static final String SQL_QUERY_DELETE_ROLES_FOR_GROUP = "DELETE FROM mylutece_database_group_role WHERE group_key like ?";
50      private static final String SQL_QUERY_INSERT_ROLE_FOR_GROUP = "INSERT INTO mylutece_database_group_role ( group_key, role_key ) VALUES ( ?, ? ) ";
51  
52      /**
53       * Find group's roles
54       *
55       * @param strGroupKey
56       *            the group key
57       * @param plugin
58       *            Plugin
59       * @return ArrayList the roles key list corresponding to the group
60       */
61      @Override
62      public List<String> selectGroupRoles( String strGroupKey, Plugin plugin )
63      {
64          ArrayList<String> arrayRoles = new ArrayList<>( );
65          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ROLES_FROM_GROUP_ID, plugin ) )
66          {
67              daoUtil.setString( 1, strGroupKey );
68              daoUtil.executeQuery( );
69  
70              while ( daoUtil.next( ) )
71              {
72                  int nParam = 0;
73                  arrayRoles.add( daoUtil.getString( ++nParam ) );
74              }
75          }
76          return arrayRoles;
77      }
78  
79      /**
80       * Find group's roles
81       *
82       * @param strRoleKey
83       *            The Role key
84       * @param plugin
85       *            Plugin
86       * @return ArrayList the groups key list corresponding to the role
87       */
88      @Override
89      public List<String> selectGroupRolesByRoleKey( String strRoleKey, Plugin plugin )
90      {
91          ArrayList<String> arrayGroup = new ArrayList<>( );
92          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_GROUPS_FROM_ROLE_ID, plugin ) )
93          {
94              daoUtil.setString( 1, strRoleKey );
95              daoUtil.executeQuery( );
96  
97              while ( daoUtil.next( ) )
98              {
99                  int nParam = 0;
100                 arrayGroup.add( daoUtil.getString( ++nParam ) );
101             }
102         }
103 
104         return arrayGroup;
105     }
106 
107     /**
108      * Delete roles for a group
109      * 
110      * @param strGroupKey
111      *            The key of the group
112      * @param plugin
113      *            Plugin
114      */
115     @Override
116     public void deleteRoles( String strGroupKey, Plugin plugin )
117     {
118         int nParam = 0;
119         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ROLES_FOR_GROUP, plugin ) )
120         {
121             daoUtil.setString( ++nParam, strGroupKey );
122 
123             daoUtil.executeUpdate( );
124         }
125     }
126 
127     /**
128      * Assign a role to group
129      * 
130      * @param strGroupKey
131      *            The key of the group
132      * @param strRoleKey
133      *            The key of the role
134      * @param plugin
135      *            Plugin
136      */
137     @Override
138     public void createRole( String strGroupKey, String strRoleKey, Plugin plugin )
139     {
140         int nParam = 0;
141         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_ROLE_FOR_GROUP, plugin ) )
142         {
143             daoUtil.setString( ++nParam, strGroupKey );
144             daoUtil.setString( ++nParam, strRoleKey );
145 
146             daoUtil.executeUpdate( );
147         }
148     }
149 }