1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.command.ddl; |
7 | |
8 | import org.h2.api.ErrorCode; |
9 | import org.h2.command.CommandInterface; |
10 | import org.h2.engine.Database; |
11 | import org.h2.engine.Session; |
12 | import org.h2.engine.User; |
13 | import org.h2.expression.Expression; |
14 | import org.h2.message.DbException; |
15 | import org.h2.security.SHA256; |
16 | import org.h2.util.StringUtils; |
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 Expression password; |
27 | private Expression salt; |
28 | private Expression 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(Expression password) { |
45 | this.password = password; |
46 | } |
47 | |
48 | private char[] getCharArray(Expression e) { |
49 | return e.optimize(session).getValue(session).getString().toCharArray(); |
50 | } |
51 | |
52 | private byte[] getByteArray(Expression e) { |
53 | return StringUtils.convertHexToBytes( |
54 | e.optimize(session).getValue(session).getString()); |
55 | } |
56 | |
57 | @Override |
58 | public int update() { |
59 | session.getUser().checkAdmin(); |
60 | session.commit(true); |
61 | Database db = session.getDatabase(); |
62 | if (db.findRole(userName) != null) { |
63 | throw DbException.get(ErrorCode.ROLE_ALREADY_EXISTS_1, userName); |
64 | } |
65 | if (db.findUser(userName) != null) { |
66 | if (ifNotExists) { |
67 | return 0; |
68 | } |
69 | throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, userName); |
70 | } |
71 | int id = getObjectId(); |
72 | User user = new User(db, id, userName, false); |
73 | user.setAdmin(admin); |
74 | user.setComment(comment); |
75 | if (hash != null && salt != null) { |
76 | user.setSaltAndHash(getByteArray(salt), getByteArray(hash)); |
77 | } else if (password != null) { |
78 | char[] passwordChars = getCharArray(password); |
79 | byte[] userPasswordHash; |
80 | if (userName.length() == 0 && passwordChars.length == 0) { |
81 | userPasswordHash = new byte[0]; |
82 | } else { |
83 | userPasswordHash = SHA256.getKeyPasswordHash(userName, passwordChars); |
84 | } |
85 | user.setUserPasswordHash(userPasswordHash); |
86 | } else { |
87 | throw DbException.throwInternalError(); |
88 | } |
89 | db.addDatabaseObject(session, user); |
90 | return 0; |
91 | } |
92 | |
93 | public void setSalt(Expression e) { |
94 | salt = e; |
95 | } |
96 | |
97 | public void setHash(Expression e) { |
98 | hash = e; |
99 | } |
100 | |
101 | public void setAdmin(boolean b) { |
102 | admin = b; |
103 | } |
104 | |
105 | public void setComment(String comment) { |
106 | this.comment = comment; |
107 | } |
108 | |
109 | @Override |
110 | public int getType() { |
111 | return CommandInterface.CREATE_USER; |
112 | } |
113 | |
114 | } |