File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\AlterUser.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 statements
21 * ALTER USER ADMIN,
22 * ALTER USER RENAME,
23 * ALTER USER SET PASSWORD
24 */
25 public class AlterUser extends DefineCommand {
26
27 private int type;
28 private User user;
29 private String newName;
30 private Expression password;
31 private Expression salt;
32 private Expression hash;
33 private boolean admin;
34
35 public AlterUser(Session session) {
36 super(session);
37 }
38
39 public void setType(int type) {
40 this.type = type;
41 }
42
43 public void setNewName(String newName) {
44 this.newName = newName;
45 }
46
47 public void setUser(User user) {
48 this.user = user;
49 }
50
51 public void setAdmin(boolean admin) {
52 this.admin = admin;
53 }
54
55 public void setSalt(Expression e) {
56 salt = e;
57 }
58
59 public void setHash(Expression e) {
60 hash = e;
61 }
62
63 public void setPassword(Expression password) {
64 this.password = password;
65 }
66
67 private char[] getCharArray(Expression e) {
68 return e.optimize(session).getValue(session).getString().toCharArray();
69 }
70
71 private byte[] getByteArray(Expression e) {
72 return StringUtils.convertHexToBytes(e.optimize(session).getValue(session).getString());
73 }
74
75 public int update() {
76 session.commit(true);
77 Database db = session.getDatabase();
78 switch (type) {
79 case CommandInterface.ALTER_USER_SET_PASSWORD:
80 if (user != session.getUser()) {
81 session.getUser().checkAdmin();
82 }
83 if (hash != null && salt != null) {
84 user.setSaltAndHash(getByteArray(salt), getByteArray(hash));
85 } else {
86 String name = newName == null ? user.getName() : newName;
87 char[] passwordChars = getCharArray(password);
88 byte[] userPasswordHash = SHA256.getKeyPasswordHash(name, passwordChars);
89 user.setUserPasswordHash(userPasswordHash);
90 }
91 break;
92 case CommandInterface.ALTER_USER_RENAME:
93 session.getUser().checkAdmin();
94 if (db.findUser(newName) != null || newName.equals(user.getName())) {
95 throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, newName);
96 }
97 db.renameDatabaseObject(session, user, newName);
98 break;
99 case CommandInterface.ALTER_USER_ADMIN:
100 session.getUser().checkAdmin();
101 if (!admin) {
102 user.checkOwnsNoSchemas();
103 }
104 user.setAdmin(admin);
105 break;
106 default:
107 DbException.throwInternalError("type=" + type);
108 }
109 db.update(session, user);
110 return 0;
111 }
112
113 public int getType() {
114 return type;
115 }
116
117 }
118