1 24 25 package com.mckoi.database.interpret; 26 27 import com.mckoi.database.*; 28 import java.util.List ; 29 30 36 37 public class UserManager extends Statement { 38 39 40 43 private void internalSetUserGroupsAndLock( 44 DatabaseQueryContext context, String username, 45 Expression[] groups_list, String lock_status) 46 throws DatabaseException { 47 48 Database db = context.getDatabase(); 49 50 if (groups_list != null) { 52 db.deleteAllUserGroups(context, username); 54 for (int i = 0; i < groups_list.length; ++i) { 55 TObject group_tob = groups_list[i].evaluate(null, null, context); 56 String group_str = group_tob.getObject().toString(); 57 db.addUserToGroup(context, username, group_str); 58 } 59 } 60 61 if (lock_status != null) { 63 if (lock_status.equals("LOCK")) { 64 db.setUserLock(context, user, true); 65 } 66 else { 67 db.setUserLock(context, user, false); 68 } 69 } 70 71 } 72 73 76 private void internalCreateUser( 77 DatabaseQueryContext context, String username, String password_str, 78 Expression[] groups_list, String lock_status) 79 throws DatabaseException { 80 81 Database db = context.getDatabase(); 83 db.createUser(context, username, password_str); 84 85 internalSetUserGroupsAndLock(context, username, groups_list, lock_status); 86 87 db.grantHostAccessToUser(context, username, "TCP", "%"); 90 db.grantHostAccessToUser(context, username, "Local", "%"); 92 93 } 94 95 97 public void prepare() throws DatabaseException { 98 } 100 101 public Table evaluate() throws DatabaseException { 102 103 DatabaseQueryContext context = new DatabaseQueryContext(database); 104 105 String command_type = (String ) cmd.getObject("type"); 106 String username = (String ) cmd.getObject("username"); 107 108 boolean modify_own_record = command_type.equals("ALTER USER") && 110 user.getUserName().equals(username); 111 boolean secure_access_privs = 113 context.getDatabase().canUserCreateAndDropUsers(context, user); 114 115 if (!(modify_own_record || secure_access_privs)) { 118 throw new DatabaseException( 119 "User is not permitted to create, alter or drop user."); 120 } 121 122 if (username.equalsIgnoreCase("public")) { 123 throw new DatabaseException("Username 'public' is reserved."); 124 } 125 126 if (command_type.equals("CREATE USER") || 128 command_type.equals("ALTER USER")) { 129 130 Expression password = (Expression) cmd.getObject("password_expression"); 131 Expression[] groups_list = (Expression[]) cmd.getObject("groups_list"); 132 String lock_status = (String ) cmd.getObject("lock_status"); 133 134 String password_str = null; 135 if (password != null) { 136 TObject passwd_tob = password.evaluate(null, null, context); 137 password_str = passwd_tob.getObject().toString(); 138 } 139 140 if (command_type.equals("CREATE USER")) { 141 143 Database db = context.getDatabase(); 145 if (!db.userExists(context, username)) { 146 internalCreateUser(context, username, password_str, 147 groups_list, lock_status); 148 } 149 else { 150 throw new DatabaseException( 151 "User '" + username + "' already exists."); 152 } 153 154 } 155 else if (command_type.equals("ALTER USER")) { 156 158 if (!secure_access_privs) { 163 if (groups_list != null) { 164 throw new DatabaseException( 165 "User is not permitted to alter user groups."); 166 } 167 if (lock_status != null) { 168 throw new DatabaseException( 169 "User is not permitted to alter user lock status."); 170 } 171 } 172 173 Database db = context.getDatabase(); 174 if (db.userExists(context, username)) { 175 if (password_str != null) { 176 db.alterUserPassword(context, username, password_str); 177 } 178 internalSetUserGroupsAndLock(context, username, 179 groups_list, lock_status); 180 } 181 else { 182 throw new DatabaseException("User '" + username + "' doesn't exist."); 183 } 184 } 185 186 } 187 else if (command_type.equals("DROP USER")) { 188 Database db = context.getDatabase(); 189 if (db.userExists(context, username)) { 190 db.deleteUser(context, username); 192 } 193 else { 194 throw new DatabaseException("User '" + username + "' doesn't exist."); 195 } 196 } 197 else { 198 throw new DatabaseException("Unknown user manager command: " + 199 command_type); 200 } 201 202 return FunctionTable.resultTable(context, 0); 203 } 204 205 206 } 207 208 | Popular Tags |