1 21 22 package org.apache.derby.database; 23 import org.apache.derby.iapi.db.PropertyInfo; 24 import org.apache.derby.iapi.store.access.TransactionController; 25 import org.apache.derby.iapi.util.IdUtil; 26 import org.apache.derby.iapi.error.StandardException; 27 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 28 import org.apache.derby.iapi.sql.conn.ConnectionUtil; 29 import org.apache.derby.iapi.reference.SQLState; 30 import org.apache.derby.iapi.reference.Property; 31 32 import java.sql.SQLException ; 33 import org.apache.derby.iapi.error.PublicAPI; 34 35 41 public abstract class UserUtility 42 { 43 44 public final static String READ_ACCESS_PERMISSION = "READ_ACCESS_PERMISSION"; 45 46 public final static String FULL_ACCESS_PERMISSION = "FULL_ACCESS_PERMISSION"; 47 48 49 private UserUtility() {} 50 51 62 public static final void add(String userName, String permission) 63 throws SQLException 64 { 65 String pv; 66 TransactionController tc = ConnectionUtil.getCurrentLCC().getTransactionExecute(); 67 try { 68 normalizeIdParam("userName",userName); if (permission==null) 70 throw StandardException.newException(SQLState.UU_INVALID_PARAMETER, "permission","null"); 71 if (permission.equals(READ_ACCESS_PERMISSION)) 72 { 73 pv = (String )tc.getProperty(Property.READ_ONLY_ACCESS_USERS_PROPERTY); 74 pv = IdUtil.appendId(userName,pv); 75 PropertyInfo.setDatabaseProperty(Property.READ_ONLY_ACCESS_USERS_PROPERTY,pv); 76 } 77 else if (permission.equals(FULL_ACCESS_PERMISSION)) 78 { 79 pv = (String )tc.getProperty(Property.FULL_ACCESS_USERS_PROPERTY); 80 pv = IdUtil.appendId(userName,pv); 81 PropertyInfo.setDatabaseProperty(Property.FULL_ACCESS_USERS_PROPERTY,pv); 82 } 83 else 84 throw StandardException.newException(SQLState.UU_UNKNOWN_PERMISSION, permission); 85 } catch (StandardException se) { 86 throw PublicAPI.wrapStandardException(se); 87 } 88 } 89 90 101 public static final void set(String userName, String permission) 102 throws SQLException 103 { 104 drop(userName); 105 add(userName,permission); 106 } 107 108 120 public static final void drop(String userName) throws 121 SQLException 122 { 123 TransactionController tc = ConnectionUtil.getCurrentLCC().getTransactionExecute(); 124 125 try { 126 String userId = normalizeIdParam("userName",userName); 127 128 String access = getPermission(userName); 129 if (access != null && access.equals(READ_ACCESS_PERMISSION)) 130 { 131 String pv = (String )tc.getProperty(Property.READ_ONLY_ACCESS_USERS_PROPERTY); 132 String newList = IdUtil.deleteId(userId,pv); 133 PropertyInfo.setDatabaseProperty(Property.READ_ONLY_ACCESS_USERS_PROPERTY,newList); 134 } 135 else if (access != null && access.equals(FULL_ACCESS_PERMISSION)) 136 { 137 String pv = (String )tc.getProperty(Property.FULL_ACCESS_USERS_PROPERTY); 138 String newList = IdUtil.deleteId(userId,pv); 139 PropertyInfo.setDatabaseProperty(Property.FULL_ACCESS_USERS_PROPERTY,newList); 140 } 141 else 142 { 143 throw StandardException.newException(SQLState.UU_UNKNOWN_USER, userName); 144 } 145 } catch (StandardException se) { 146 throw PublicAPI.wrapStandardException(se); 147 } 148 } 149 150 164 public static final String getPermission(String userName) 165 throws SQLException 166 { 167 TransactionController tc = ConnectionUtil.getCurrentLCC().getTransactionExecute(); 168 169 try { 170 171 String pv = (String ) 172 tc.getProperty(Property.READ_ONLY_ACCESS_USERS_PROPERTY); 173 String userId = normalizeIdParam("userName",userName); 174 if (IdUtil.idOnList(userId,pv)) return READ_ACCESS_PERMISSION; 175 pv = (String )tc.getProperty(Property.FULL_ACCESS_USERS_PROPERTY); 176 if (IdUtil.idOnList(userId,pv)) return FULL_ACCESS_PERMISSION; 177 return null; 178 } catch (StandardException se) { 179 throw PublicAPI.wrapStandardException(se); 180 } 181 } 182 183 private static String normalizeIdParam(String pName, String pValue) 184 throws StandardException 185 { 186 if (pValue==null) 187 throw StandardException.newException(SQLState.UU_INVALID_PARAMETER, pName,"null"); 188 189 try { 190 return IdUtil.parseId(pValue); 191 } 192 catch (StandardException se) { 193 throw StandardException.newException(SQLState.UU_INVALID_PARAMETER, se, pName,pValue); 194 } 195 } 196 } 197 | Popular Tags |