1 5 package org.h2.engine; 6 7 import java.sql.SQLException ; 8 9 import org.h2.message.Message; 10 import org.h2.message.Trace; 11 import org.h2.security.SHA256; 12 import org.h2.table.MetaTable; 13 import org.h2.table.RangeTable; 14 import org.h2.table.Table; 15 import org.h2.util.ByteUtils; 16 import org.h2.util.ObjectArray; 17 import org.h2.util.RandomUtils; 18 import org.h2.util.StringUtils; 19 20 public class User extends RightOwner { 21 22 private byte[] salt; 23 private byte[] passwordHash; 24 private boolean admin; 25 private boolean systemUser; 26 27 public User(Database database, int id, String userName, boolean systemUser) { 28 super(database, id, userName, Trace.USER); 29 this.systemUser = systemUser; 30 } 31 32 public void setAdmin(boolean admin) { 33 this.admin = admin; 34 } 35 36 public boolean getAdmin() { 37 return admin; 38 } 39 40 public void setSaltAndHash(byte[] salt, byte[] hash) { 41 this.salt = salt; 42 this.passwordHash = hash; 43 } 44 45 public void setUserPasswordHash(byte[] userPasswordHash) { 46 if (userPasswordHash != null) { 47 salt = RandomUtils.getSecureBytes(Constants.SALT_LEN); 48 SHA256 sha = new SHA256(); 49 this.passwordHash = sha.getHashWithSalt(userPasswordHash, salt); 50 } 51 } 52 53 public String getCreateSQLForCopy(Table table, String quotedName) { 54 throw Message.getInternalError(); 55 } 56 57 public String getCreateSQL() { 58 return getCreateSQL(true, false); 59 } 60 61 public void checkRight(Table table, int rightMask) throws SQLException { 62 if(rightMask != Right.SELECT && !systemUser) { 63 database.checkWritingAllowed(); 64 } 65 if(admin) { 66 return; 67 } 68 Role publicRole = database.getPublicRole(); 69 if(publicRole.isRightGrantedRecursive(table, rightMask)) { 70 return; 71 } 72 if(table instanceof MetaTable || table instanceof RangeTable) { 73 return; 75 } 76 if(!isRightGrantedRecursive(table, rightMask)) { 77 throw Message.getSQLException(Message.NOT_ENOUGH_RIGHTS_FOR_1, table.getSQL()); 78 } 79 } 80 81 public String getCreateSQL(boolean password, boolean ifNotExists) { 82 StringBuffer buff = new StringBuffer (); 83 buff.append("CREATE USER "); 84 if(ifNotExists) { 85 buff.append("IF NOT EXISTS "); 86 } 87 buff.append(getSQL()); 88 if(comment != null) { 89 buff.append(" COMMENT "); 90 buff.append(StringUtils.quoteStringSQL(comment)); 91 } 92 if(password) { 93 buff.append(" SALT '"); 94 buff.append(ByteUtils.convertBytesToString(salt)); 95 buff.append("' HASH '"); 96 buff.append(ByteUtils.convertBytesToString(passwordHash)); 97 buff.append("'"); 98 } else { 99 buff.append(" PASSWORD ''"); 100 } 101 if(admin) { 102 buff.append(" ADMIN"); 103 } 104 return buff.toString(); 105 } 106 107 public void checkUserPasswordHash(byte[] buff) throws SQLException { 108 SHA256 sha = new SHA256(); 109 byte[] hash = sha.getHashWithSalt(buff, salt); 110 if(!ByteUtils.compareSecure(hash, passwordHash)) { 111 throw Message.getSQLException(Message.WRONG_USER_OR_PASSWORD); 112 } 113 } 114 115 public void checkAdmin() throws SQLException { 116 if(!admin) { 117 throw Message.getSQLException(Message.ADMIN_RIGHTS_REQUIRED); 118 } 119 } 120 121 public int getType() { 122 return DbObject.USER; 123 } 124 125 public ObjectArray getChildren() { 126 ObjectArray all = database.getAllRights(); 127 ObjectArray rights = new ObjectArray(); 128 for(int i=0; i<all.size(); i++) { 129 Right right = (Right) all.get(i); 130 if(right.getGrantee() == this) { 131 rights.add(right); 132 } 133 } 134 return rights; 135 } 136 137 public void removeChildrenAndResources(Session session) throws SQLException { 138 ObjectArray rights = database.getAllRights(); 139 for(int i=0; i<rights.size(); i++) { 140 Right right = (Right) rights.get(i); 141 if(right.getGrantee() == this) { 142 database.removeDatabaseObject(session, right); 143 } 144 } 145 salt = null; 146 ByteUtils.clear(passwordHash); 147 passwordHash = null; 148 invalidate(); 149 } 150 151 public void checkRename() { 152 } 154 155 } 156 | Popular Tags |