File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\CreateUser.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.ddl;
8
9 import org.h2.command.CommandInterface;
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.expression.Expression;
15 import org.h2.message.DbException;
16 import org.h2.security.SHA256;
17 import org.h2.util.StringUtils;
18
19 /**
20 * This class represents the statement
21 * CREATE USER
22 */
23 public class CreateUser extends DefineCommand {
24
25 private String userName;
26 private boolean admin;
27 private Expression password;
28 private Expression salt;
29 private Expression hash;
30 private boolean ifNotExists;
31 private String comment;
32
33 public CreateUser(Session session) {
34 super(session);
35 }
36
37 public void setIfNotExists(boolean ifNotExists) {
38 this.ifNotExists = ifNotExists;
39 }
40
41 public void setUserName(String userName) {
42 this.userName = userName;
43 }
44
45 public void setPassword(Expression password) {
46 this.password = password;
47 }
48
49 private char[] getCharArray(Expression e) {
50 return e.optimize(session).getValue(session).getString().toCharArray();
51 }
52
53 private byte[] getByteArray(Expression e) {
54 return StringUtils.convertHexToBytes(e.optimize(session).getValue(session).getString());
55 }
56
57 public int update() {
58 session.getUser().checkAdmin();
59 session.commit(true);
60 Database db = session.getDatabase();
61 if (db.findRole(userName) != null) {
62 throw DbException.get(ErrorCode.ROLE_ALREADY_EXISTS_1, userName);
63 }
64 if (db.findUser(userName) != null) {
65 if (ifNotExists) {
66 return 0;
67 }
68 throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, userName);
69 }
70 int id = getObjectId();
71 User user = new User(db, id, userName, false);
72 user.setAdmin(admin);
73 user.setComment(comment);
74 if (hash != null && salt != null) {
75 user.setSaltAndHash(getByteArray(salt), getByteArray(hash));
76 } else if (password != null) {
77 char[] passwordChars = getCharArray(password);
78 byte[] userPasswordHash;
79 if (userName.length() == 0 && passwordChars.length == 0) {
80 userPasswordHash = new byte[0];
81 } else {
82 userPasswordHash = SHA256.getKeyPasswordHash(userName, passwordChars);
83 }
84 user.setUserPasswordHash(userPasswordHash);
85 } else {
86 throw DbException.throwInternalError();
87 }
88 db.addDatabaseObject(session, user);
89 return 0;
90 }
91
92 public void setSalt(Expression e) {
93 salt = e;
94 }
95
96 public void setHash(Expression e) {
97 hash = e;
98 }
99
100 public void setAdmin(boolean b) {
101 admin = b;
102 }
103
104 public void setComment(String comment) {
105 this.comment = comment;
106 }
107
108 public int getType() {
109 return CommandInterface.CREATE_USER;
110 }
111
112 }
113