01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.ddl;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.constant.ErrorCode;
11: import org.h2.engine.Database;
12: import org.h2.engine.Session;
13: import org.h2.engine.User;
14: import org.h2.message.Message;
15: import org.h2.security.SHA256;
16: import org.h2.util.ByteUtils;
17:
18: /**
19: * This class represents the statement
20: * CREATE USER
21: */
22: public class CreateUser extends DefineCommand {
23:
24: private String userName;
25: private boolean admin;
26: private byte[] userPasswordHash;
27: private byte[] salt;
28: private byte[] hash;
29: private boolean ifNotExists;
30: private String comment;
31:
32: public CreateUser(Session session) {
33: super (session);
34: }
35:
36: public void setIfNotExists(boolean ifNotExists) {
37: this .ifNotExists = ifNotExists;
38: }
39:
40: public void setUserName(String userName) {
41: this .userName = userName;
42: }
43:
44: public void setPassword(String password) {
45: SHA256 sha = new SHA256();
46: this .userPasswordHash = sha.getKeyPasswordHash(userName,
47: password.toCharArray());
48: }
49:
50: public int update() throws SQLException {
51: session.getUser().checkAdmin();
52: session.commit(true);
53: Database db = session.getDatabase();
54: if (db.findUser(userName) != null) {
55: if (ifNotExists) {
56: return 0;
57: }
58: throw Message.getSQLException(
59: ErrorCode.USER_ALREADY_EXISTS_1, userName);
60: }
61: int id = getObjectId(false, true);
62: User user = new User(db, id, userName, false);
63: user.setAdmin(admin);
64: user.setComment(comment);
65: if (hash != null && salt != null) {
66: user.setSaltAndHash(salt, hash);
67: } else {
68: user.setUserPasswordHash(userPasswordHash);
69: }
70: db.addDatabaseObject(session, user);
71: return 0;
72: }
73:
74: public void setSalt(String s) throws SQLException {
75: salt = ByteUtils.convertStringToBytes(s);
76: }
77:
78: public void setHash(String s) throws SQLException {
79: hash = ByteUtils.convertStringToBytes(s);
80: }
81:
82: public void setAdmin(boolean b) {
83: admin = b;
84: }
85:
86: public void setComment(String comment) {
87: this.comment = comment;
88: }
89:
90: }
|