001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.command.ddl;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.constant.ErrorCode;
011: import org.h2.engine.Database;
012: import org.h2.engine.Session;
013: import org.h2.engine.User;
014: import org.h2.message.Message;
015: import org.h2.security.SHA256;
016: import org.h2.util.ByteUtils;
017:
018: /**
019: * This class represents the statements
020: * ALTER USER ADMIN,
021: * ALTER USER RENAME,
022: * ALTER USER SET PASSWORD
023: */
024: public class AlterUser extends DefineCommand {
025:
026: /**
027: * The command type to set the password.
028: */
029: public static final int SET_PASSWORD = 0;
030:
031: /**
032: * The command type to rename the user.
033: */
034: public static final int RENAME = 1;
035:
036: /**
037: * The command type to change the admin flag.
038: */
039: public static final int ADMIN = 2;
040:
041: private int type;
042: private User user;
043: private String newName;
044: private byte[] userPasswordHash;
045: private byte[] salt;
046: private byte[] hash;
047: private boolean admin;
048:
049: public AlterUser(Session session) {
050: super (session);
051: }
052:
053: public void setType(int type) {
054: this .type = type;
055: }
056:
057: public void setNewName(String newName) {
058: this .newName = newName;
059: }
060:
061: public void setUser(User user) {
062: this .user = user;
063: }
064:
065: public void setAdmin(boolean admin) {
066: this .admin = admin;
067: }
068:
069: public void setSalt(String s) throws SQLException {
070: salt = ByteUtils.convertStringToBytes(s);
071: }
072:
073: public void setHash(String s) throws SQLException {
074: hash = ByteUtils.convertStringToBytes(s);
075: }
076:
077: public void setPassword(String password) {
078: SHA256 sha = new SHA256();
079: String name = newName == null ? user.getName() : newName;
080: this .userPasswordHash = sha.getKeyPasswordHash(name, password
081: .toCharArray());
082: }
083:
084: public int update() throws SQLException {
085: session.commit(true);
086: Database db = session.getDatabase();
087: switch (type) {
088: case SET_PASSWORD:
089: if (user != session.getUser()) {
090: session.getUser().checkAdmin();
091: }
092: if (hash != null && salt != null) {
093: user.setSaltAndHash(salt, hash);
094: } else {
095: user.setUserPasswordHash(userPasswordHash);
096: }
097: break;
098: case RENAME:
099: session.getUser().checkAdmin();
100: if (db.findUser(newName) != null
101: || newName.equals(user.getName())) {
102: throw Message.getSQLException(
103: ErrorCode.USER_ALREADY_EXISTS_1, newName);
104: }
105: db.renameDatabaseObject(session, user, newName);
106: break;
107: case ADMIN:
108: session.getUser().checkAdmin();
109: if (!admin) {
110: user.checkNoSchemas();
111: }
112: user.setAdmin(admin);
113: break;
114: default:
115: throw Message.getInternalError("type=" + type);
116: }
117: db.update(session, user);
118: return 0;
119: }
120:
121: }
|