| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://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.SessionLocal; |
| 12 |
import org.h2.engine.User; |
| 13 |
import org.h2.expression.Expression; |
| 14 |
import org.h2.message.DbException; |
| 15 |
|
| 16 |
/** |
| 17 |
* This class represents the statements |
| 18 |
* ALTER USER ADMIN, |
| 19 |
* ALTER USER RENAME, |
| 20 |
* ALTER USER SET PASSWORD |
| 21 |
*/ |
| 22 |
public class AlterUser extends DefineCommand { |
| 23 |
|
| 24 |
private int type; |
| 25 |
private User user; |
| 26 |
private String newName; |
| 27 |
private Expression password; |
| 28 |
private Expression salt; |
| 29 |
private Expression hash; |
| 30 |
private boolean admin; |
| 31 |
|
| 32 |
public AlterUser(SessionLocal session) { |
| 33 |
super(session); |
| 34 |
} |
| 35 |
|
| 36 |
public void setType(int type) { |
| 37 |
this.type = type; |
| 38 |
} |
| 39 |
|
| 40 |
public void setNewName(String newName) { |
| 41 |
this.newName = newName; |
| 42 |
} |
| 43 |
|
| 44 |
public void setUser(User user) { |
| 45 |
this.user = user; |
| 46 |
} |
| 47 |
|
| 48 |
public void setAdmin(boolean admin) { |
| 49 |
this.admin = admin; |
| 50 |
} |
| 51 |
|
| 52 |
public void setSalt(Expression e) { |
| 53 |
salt = e; |
| 54 |
} |
| 55 |
|
| 56 |
public void setHash(Expression e) { |
| 57 |
hash = e; |
| 58 |
} |
| 59 |
|
| 60 |
public void setPassword(Expression password) { |
| 61 |
this.password = password; |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public long update() { |
| 66 |
Database db = getDatabase(); |
| 67 |
switch (type) { |
| 68 |
case CommandInterface.ALTER_USER_SET_PASSWORD: |
| 69 |
if (user != session.getUser()) { |
| 70 |
session.getUser().checkAdmin(); |
| 71 |
} |
| 72 |
if (hash != null && salt != null) { |
| 73 |
CreateUser.setSaltAndHash(user, session, salt, hash); |
| 74 |
} else { |
| 75 |
CreateUser.setPassword(user, session, password); |
| 76 |
} |
| 77 |
break; |
| 78 |
case CommandInterface.ALTER_USER_RENAME: |
| 79 |
session.getUser().checkAdmin(); |
| 80 |
if (db.findUser(newName) != null || newName.equals(user.getName())) { |
| 81 |
throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, newName); |
| 82 |
} |
| 83 |
db.renameDatabaseObject(session, user, newName); |
| 84 |
break; |
| 85 |
case CommandInterface.ALTER_USER_ADMIN: |
| 86 |
session.getUser().checkAdmin(); |
| 87 |
user.setAdmin(admin); |
| 88 |
break; |
| 89 |
default: |
| 90 |
throw DbException.getInternalError("type=" + type); |
| 91 |
} |
| 92 |
db.updateMeta(session, user); |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public int getType() { |
| 98 |
return type; |
| 99 |
} |
| 100 |
|
| 101 |
} |