11/**
22 * Copyright 2013 Sean Kavanagh - [email protected] 3- *
3+ * <p/>
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
7- *
7+ * <p/>
88 * http://www.apache.org/licenses/LICENSE-2.0
9- *
9+ * <p/>
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1212 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616package com .keybox .manage .db ;
1717
1818import com .keybox .manage .model .Profile ;
19- import com .keybox .manage .model .User ;
2019import com .keybox .manage .util .DBUtils ;
2120
2221import java .sql .Connection ;
2322import java .sql .PreparedStatement ;
2423import java .sql .ResultSet ;
2524import java .util .ArrayList ;
2625import java .util .List ;
26+
27+ import org .apache .commons .lang3 .StringUtils ;
2728import org .slf4j .Logger ;
2829import org .slf4j .LoggerFactory ;
2930
@@ -39,12 +40,12 @@ private UserProfileDB() {
3940
4041 /**
4142 * sets users for profile
42- *
43- * @param profileId profile id
43+ *
44+ * @param profileId profile id
4445 * @param userIdList list of user ids
4546 */
4647 public static void setUsersForProfile (Long profileId , List <Long > userIdList ) {
47-
48+
4849 Connection con = null ;
4950 PreparedStatement stmt = null ;
5051
@@ -55,7 +56,7 @@ public static void setUsersForProfile(Long profileId, List<Long> userIdList) {
5556 stmt .execute ();
5657 DBUtils .closeStmt (stmt );
5758
58- for (Long userId : userIdList ) {
59+ for (Long userId : userIdList ) {
5960 stmt = con .prepareStatement ("insert into user_map (profile_id, user_id) values (?,?)" );
6061 stmt .setLong (1 , profileId );
6162 stmt .setLong (2 , userId );
@@ -67,14 +68,14 @@ public static void setUsersForProfile(Long profileId, List<Long> userIdList) {
6768
6869 } catch (Exception e ) {
6970 log .error (e .toString (), e );
70- }
71- finally {
71+ } finally {
7272 DBUtils .closeConn (con );
7373 }
7474 }
7575
7676 /**
7777 * return a list of profiles for user
78+ *
7879 * @param userId user id
7980 * @return profile list
8081 */
@@ -89,8 +90,7 @@ public static List<Profile> getProfilesByUser(Long userId) {
8990
9091 } catch (Exception e ) {
9192 log .error (e .toString (), e );
92- }
93- finally {
93+ } finally {
9494 DBUtils .closeConn (con );
9595 }
9696 return profileList ;
@@ -100,6 +100,7 @@ public static List<Profile> getProfilesByUser(Long userId) {
100100
101101 /**
102102 * return a list of profiles for user
103+ *
103104 * @param userId user id
104105 * @return profile list
105106 */
@@ -133,12 +134,12 @@ public static List<Profile> getProfilesByUser(Connection con, Long userId) {
133134 /**
134135 * checks to determine if user belongs to profile
135136 *
136- * @param userId user id
137+ * @param userId user id
137138 * @param profileId profile id
138139 * @return true if user belongs to profile
139140 */
140- public static boolean checkIsUsersProfile (Long userId , Long profileId ){
141- boolean isUsersProfile = false ;
141+ public static boolean checkIsUsersProfile (Long userId , Long profileId ) {
142+ boolean isUsersProfile = false ;
142143
143144 Connection con = null ;
144145
@@ -151,21 +152,72 @@ public static boolean checkIsUsersProfile(Long userId, Long profileId){
151152 ResultSet rs = stmt .executeQuery ();
152153
153154 while (rs .next ()) {
154- isUsersProfile = true ;
155+ isUsersProfile = true ;
155156 }
156157 DBUtils .closeRs (rs );
157158 DBUtils .closeStmt (stmt );
158159 DBUtils .closeStmt (stmt );
159160
160161 } catch (Exception e ) {
161162 log .error (e .toString (), e );
162- }
163- finally {
163+ } finally {
164164 DBUtils .closeConn (con );
165165 }
166166
167167 return isUsersProfile ;
168168
169169 }
170170
171+ /**
172+ * assigns profiles to given user
173+ *
174+ * @param userId user id
175+ * @param allProfilesNmList list of all profiles
176+ * @param assignedProfilesNmList list of assigned profiles
177+ */
178+ public static void assignProfilesToUser (Connection con , Long userId , List <String > allProfilesNmList , List <String > assignedProfilesNmList ) {
179+
180+ PreparedStatement stmt = null ;
181+
182+ try {
183+
184+ for (String profileNm : allProfilesNmList ) {
185+ if (StringUtils .isNotEmpty (profileNm )) {
186+
187+ Long profileId = null ;
188+ stmt = con .prepareStatement ("select id from profiles p where lower(p.nm) like ?" );
189+ stmt .setString (1 , profileNm .toLowerCase ());
190+ ResultSet rs = stmt .executeQuery ();
191+ while (rs .next ()) {
192+ profileId = rs .getLong ("id" );
193+ }
194+ DBUtils .closeRs (rs );
195+ DBUtils .closeStmt (stmt );
196+
197+ if (profileId != null ) {
198+ stmt = con .prepareStatement ("delete from user_map where profile_id=?" );
199+ stmt .setLong (1 , profileId );
200+ stmt .execute ();
201+ DBUtils .closeStmt (stmt );
202+
203+ if (assignedProfilesNmList .contains (profileNm )) {
204+ stmt = con .prepareStatement ("insert into user_map (profile_id, user_id) values (?,?)" );
205+ stmt .setLong (1 , profileId );
206+ stmt .setLong (2 , userId );
207+ stmt .execute ();
208+ DBUtils .closeStmt (stmt );
209+ }
210+
211+ //delete all unassigned keys by profile
212+ PublicKeyDB .deleteUnassignedKeysByProfile (con , profileId );
213+ }
214+
215+ }
216+ }
217+
218+ } catch (Exception e ) {
219+ log .error (e .toString (), e );
220+ }
221+ }
222+
171223}
0 commit comments