1 13 package org.jahia.services.usermanager; 14 15 import java.sql.Connection ; 16 import java.sql.PreparedStatement ; 17 import java.sql.ResultSet ; 18 import java.sql.SQLException ; 19 import java.sql.Statement ; 20 import java.util.Enumeration ; 21 import java.util.HashSet ; 22 import java.util.Properties ; 23 import java.util.Set ; 24 25 import org.jahia.exceptions.JahiaException; 26 import org.jahia.exceptions.database.JahiaDatabaseConnectionException; 27 import org.jahia.exceptions.database.JahiaDatabaseException; 28 import org.jahia.utils.JahiaTools; 29 30 31 41 class JahiaUserDBUtils { 42 static private JahiaUserDBUtils mObject = null; 43 44 private static final String USERNAME_PROPERTY_NAME = "username"; 45 46 private static org.apache.log4j.Logger logger = 47 org.apache.log4j.Logger.getLogger (JahiaUserDBUtils.class); 48 49 private JahiaUserDBUtils () { 51 } 52 53 54 60 static public JahiaUserDBUtils getInstance () { 61 if (mObject == null) { 62 mObject = new JahiaUserDBUtils (); 63 } 64 return mObject; 65 } 66 67 public UserProperties getUserProperties (int userID, String providerName, String userKey) 68 throws JahiaDatabaseException { 69 UserProperties properties = new UserProperties (); 71 final String query = "SELECT name_jahia_user_prop, value_jahia_user_prop FROM jahia_user_prop WHERE id_jahia_users=? and provider_jahia_user_prop=? and userkey_jahia_user_prop=?"; 72 PreparedStatement statement = null; 73 try { 74 75 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 76 if (dbConn == null) { 77 return null; 78 } 79 statement = dbConn.prepareStatement (query); 81 statement.setInt (1, userID); 82 statement.setString (2, providerName); 83 statement.setString (3, userKey); 84 85 ResultSet rs = statement.executeQuery (); 86 if (rs != null) { 87 88 String propName = null; 89 String propVal = null; 90 91 while (rs.next ()) { 92 93 propName = rs.getString ("name_jahia_user_prop"); 94 propVal = rs.getString ("value_jahia_user_prop"); 95 if (propVal == null) { 96 propVal = ""; 97 } 98 99 if (propName != null) { 100 UserProperty userProperty = new UserProperty(propName, propVal, false); 101 properties.setUserProperty(propName, userProperty); 102 } 103 } 104 105 } 106 } catch (SQLException sqlEx) { 107 throw new JahiaDatabaseException ( 108 "", query, sqlEx, JahiaDatabaseException.ERROR_SEVERITY); 109 } finally { 110 closeStatement (statement); 111 } 112 113 return properties; 114 } 115 116 127 public synchronized boolean removeProperty (String propertyKey, int userID, 128 String providerName, String userKey) 129 throws JahiaDatabaseException, 130 JahiaDatabaseConnectionException, 131 JahiaException { 132 boolean result = false; 133 134 String query = "DELETE FROM jahia_user_prop WHERE" + 135 " id_jahia_users=" + userID + 136 " AND name_jahia_user_prop='" + propertyKey + "'" + 137 " AND provider_jahia_user_prop='" + providerName + "'" + 138 " AND userkey_jahia_user_prop='" + userKey + "'"; 139 140 return makeQuery (query); 141 } 142 143 144 156 public synchronized boolean setPassword (String password, int userID) 157 throws JahiaDatabaseException, 158 JahiaDatabaseConnectionException, 159 JahiaException { 160 boolean result = false; 161 162 if (password != null) { 164 if (password.length () > 0) { 166 String tmp = JahiaUserManagerService. 168 encryptPassword (password); 169 170 String query = "UPDATE jahia_users SET password_jahia_users='" + 171 tmp + "' WHERE id_jahia_users=" + userID; 172 result = makeQuery (query); 173 } 174 } 175 return result; 176 } 177 178 179 192 public synchronized boolean addProperty (String key, String value, 193 int userID, String providerName, String userKey) 194 throws JahiaDatabaseException, 195 JahiaDatabaseConnectionException, 196 JahiaException { 197 boolean result = false; 198 199 String query = "INSERT INTO jahia_user_prop (id_jahia_users, " + 200 "name_jahia_user_prop, value_jahia_user_prop, provider_jahia_user_prop, userkey_jahia_user_prop)" + 201 " VALUES (" + userID + ",'" + key + "','" + value + "', '" + providerName + "', '" + userKey + "')"; 202 203 return makeQuery (query); 204 } 205 206 207 220 public synchronized boolean updateProperty (String key, String value, 221 int userID, String providerName, 222 String userKey) 223 throws JahiaDatabaseException, 224 JahiaDatabaseConnectionException, 225 JahiaException { 226 boolean result = false; 227 228 String query = "UPDATE jahia_user_prop SET " + 229 "value_jahia_user_prop='" + value + "'" + 230 " WHERE id_jahia_users=" + userID + 231 " AND name_jahia_user_prop='" + key + "'" + 232 " AND provider_jahia_user_prop='" + providerName + "'" + 233 " AND userkey_jahia_user_prop='" + userKey + "'"; 234 235 return makeQuery (query); 236 } 237 238 247 private String makeLIKEString (String input) { 248 String result = JahiaTools.replacePattern (input, "*", "%"); 249 result = JahiaTools.replacePattern (result, "'", "\\'"); 250 result = JahiaTools.replacePattern (result, "\"", "\\\""); 251 result = JahiaTools.replacePattern (result, "_", "\\_"); 252 return result; 253 } 254 255 271 public synchronized Set searchUsersByProperties(int siteID, Properties searchCriterias) 272 throws JahiaDatabaseException { 273 Set userKeys = new HashSet (); 274 275 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 277 if (dbConn == null) { 278 return userKeys; 279 } 280 281 if (searchCriterias == null) { 282 searchCriterias = new Properties (); 283 searchCriterias.setProperty ("*", "*"); 284 } 285 286 boolean haveWildCardProperty = false; 287 if (searchCriterias.getProperty ("*") != null) { 288 haveWildCardProperty = true; 289 } 290 291 Statement statement = null; 293 try { 294 statement = dbConn.createStatement (); 295 if (statement != null) { 296 297 StringBuffer query; 298 boolean onlyUserNameInSelect = false; 299 if ((searchCriterias.getProperty (USERNAME_PROPERTY_NAME) != null) || 300 (haveWildCardProperty) 301 ) { 302 303 String curCriteriaValue; 304 if (haveWildCardProperty) { 305 curCriteriaValue = makeLIKEString (searchCriterias.getProperty ("*")); 306 } else { 307 curCriteriaValue = 308 makeLIKEString ( 309 searchCriterias.getProperty (USERNAME_PROPERTY_NAME)); 310 } 311 query = 312 new StringBuffer ( 313 "SELECT DISTINCT key_jahia_users AS result_key_jahia_users FROM jahia_users,jahia_sites_users WHERE "); 314 query.append ("name_jahia_users LIKE '"); 315 query.append (curCriteriaValue); 316 query.append ("'"); 317 query.append ("AND jahia_sites_users.siteid_sites_users="); 318 query.append (siteID); 319 query.append ( 320 " AND jahia_users.key_jahia_users=jahia_sites_users.userid_sites_users"); 321 ResultSet rs = statement.executeQuery (query.toString ()); 323 if (rs != null) { 324 while (rs.next ()) { 325 String name = rs.getString ("result_key_jahia_users"); 326 if (name != null) { 327 userKeys.add (name); 328 } 329 } 330 } 331 332 if ((!haveWildCardProperty) && (searchCriterias.size () == 1)) { 333 onlyUserNameInSelect = true; 334 } 335 } else { 336 onlyUserNameInSelect = false; 337 } 338 339 if (!onlyUserNameInSelect) { 340 341 query = 342 new StringBuffer ( 343 "SELECT DISTINCT jahia_users.key_jahia_users AS result_key_jahia_users FROM jahia_users, jahia_user_prop, jahia_sites_users WHERE "); 344 Enumeration criteriaNames = searchCriterias.keys (); 345 while (criteriaNames.hasMoreElements ()) { 346 String curCriteriaName = (String ) criteriaNames.nextElement (); 347 String curCriteriaValue = makeLIKEString ( 348 searchCriterias.getProperty (curCriteriaName)); 349 if ("*".equals (curCriteriaName)) { 350 query.append ("(jahia_users.name_jahia_users LIKE '"); 353 query.append (curCriteriaValue); 354 query.append ("'"); 355 query.append (" OR jahia_user_prop.value_jahia_user_prop LIKE '"); 356 query.append (curCriteriaValue); 357 query.append ("') "); 358 query.append (" AND "); 359 onlyUserNameInSelect = false; 360 } else { 361 if (USERNAME_PROPERTY_NAME.equals (curCriteriaName)) { 362 } else { 365 query.append ("(jahia_user_prop.name_jahia_user_prop='"); 366 query.append (makeLIKEString (curCriteriaName)); 367 query.append ( 368 "' AND jahia_user_prop.value_jahia_user_prop LIKE '"); 369 query.append (curCriteriaValue); 370 query.append ("') "); 371 query.append (" AND "); 372 onlyUserNameInSelect = false; 373 } 374 } 375 } 376 if (!(onlyUserNameInSelect)) { 377 if (!query.toString ().endsWith (" AND ")) { 378 query.append (" AND "); 379 } 380 } 381 query.append ( 382 "jahia_users.id_jahia_users=jahia_user_prop.id_jahia_users AND "); 383 query.append ("jahia_sites_users.siteid_sites_users="); 384 query.append (siteID); 385 query.append ( 386 " AND jahia_users.key_jahia_users=jahia_sites_users.userid_sites_users"); 387 389 ResultSet rs = statement.executeQuery (query.toString ()); 390 if (rs != null) { 391 while (rs.next ()) { 392 String name = rs.getString ("result_key_jahia_users"); 393 if (name != null) { 394 userKeys.add (name); 395 } 396 } 397 } 398 } 399 } 400 } catch (SQLException ex) { 401 logger.error ("Error while searching users in site " + siteID, ex); 402 } finally { 403 closeStatement (statement); 404 } 405 406 return userKeys; 407 408 } 409 410 411 private boolean makeQuery (String query) 416 throws JahiaDatabaseException, 417 JahiaDatabaseConnectionException, 418 JahiaException { 419 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 421 if (dbConn == null) { 422 return false; 423 } 424 425 boolean result = false; 426 Statement statement = null; 427 428 try { 429 statement = dbConn.createStatement (); 430 if (statement != null) { 431 synchronized (this) { 432 statement.executeUpdate (query); 433 result = true; 434 } 435 } 436 } catch (SQLException sqlEx) { 437 throw new JahiaDatabaseException ( 438 "", query, sqlEx, JahiaDatabaseException.ERROR_SEVERITY); 439 } finally { 440 closeStatement (statement); 441 } 442 443 return result; 444 } 445 446 private void closeStatement (Statement statement) 448 throws JahiaDatabaseException { 449 try { 451 if (statement != null) { 452 statement.close (); 453 } 454 } catch (SQLException sqlEx) { 455 throw new JahiaDatabaseException ( 456 "Could not close a statement in JahiaDBUser", 457 sqlEx, JahiaDatabaseException.ERROR_SEVERITY); 458 } 459 } 460 461 } 462 | Popular Tags |