1 5 package org.h2.command.ddl; 6 7 import java.sql.SQLException ; 8 9 import org.h2.engine.Database; 10 import org.h2.engine.Session; 11 import org.h2.engine.User; 12 import org.h2.message.Message; 13 import org.h2.security.SHA256; 14 import org.h2.util.ByteUtils; 15 16 19 20 public class AlterUser extends DefineCommand { 21 22 public static final int SET_PASSWORD = 0, RENAME = 1, ADMIN = 2; 23 private int type; 24 private User user; 25 private String newName; 26 private byte[] userPasswordHash; 27 private byte[] salt; 28 private byte[] hash; 29 private boolean admin; 30 31 public AlterUser(Session session) { 32 super(session); 33 } 34 35 public void setType(int type) { 36 this.type = type; 37 } 38 39 public void setNewName(String newName) { 40 this.newName = newName; 41 } 42 43 public void setUser(User user) { 44 this.user = user; 45 } 46 47 public void setAdmin(boolean admin) { 48 this.admin = admin; 49 } 50 51 public void setSalt(String s) throws SQLException { 52 salt = ByteUtils.convertStringToBytes(s); 53 } 54 55 public void setHash(String s) throws SQLException { 56 hash = ByteUtils.convertStringToBytes(s); 57 } 58 59 public void setPassword(String password) { 60 SHA256 sha = new SHA256(); 61 String name = newName == null ? user.getName() : newName; 62 this.userPasswordHash = sha.getKeyPasswordHash(name, password.toCharArray()); 63 } 64 65 public int update() throws SQLException { 66 session.commit(); 67 Database db = session.getDatabase(); 68 switch(type) { 69 case SET_PASSWORD: 70 if(user != session.getUser()) { 71 session.getUser().checkAdmin(); 72 } 73 if(hash!=null && salt !=null) { 74 user.setSaltAndHash(salt, hash); 75 } else { 76 user.setUserPasswordHash(userPasswordHash); 77 } 78 break; 79 case RENAME: 80 session.getUser().checkAdmin(); 81 if(db.findUser(newName) != null || newName.equals(user.getName())) { 82 throw Message.getSQLException(Message.USER_ALREADY_EXISTS_1, newName); 83 } 84 db.renameDatabaseObject(session, user, newName); 85 break; 86 case ADMIN: 87 session.getUser().checkAdmin(); 88 user.setAdmin(admin); 89 break; 90 default: 91 throw Message.getInternalError("type="+type); 92 } 93 db.update(session, user); 94 return 0; 95 } 96 97 } 98 | Popular Tags |