KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > ddl > AlterUser


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.ddl;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.engine.Database;
10 import org.h2.engine.Session;
11 import org.h2.engine.User;
12 import org.h2.message.Message;
13 import org.h2.security.SHA256;
14 import org.h2.util.ByteUtils;
15
16 /**
17  * @author Thomas
18  */

19
20 public class AlterUser extends DefineCommand {
21
22     public static final int SET_PASSWORD = 0, RENAME = 1, ADMIN = 2;
23     private int type;
24     private User user;
25     private String JavaDoc newName;
26     private byte[] userPasswordHash;
27     private byte[] salt;
28     private byte[] hash;
29     private boolean admin;
30
31     public AlterUser(Session session) {
32         super(session);
33     }
34     
35     public void setType(int type) {
36         this.type = type;
37     }
38
39     public void setNewName(String JavaDoc newName) {
40         this.newName = newName;
41     }
42
43     public void setUser(User user) {
44         this.user = user;
45     }
46
47     public void setAdmin(boolean admin) {
48         this.admin = admin;
49     }
50
51     public void setSalt(String JavaDoc s) throws SQLException JavaDoc {
52         salt = ByteUtils.convertStringToBytes(s);
53     }
54
55     public void setHash(String JavaDoc s) throws SQLException JavaDoc {
56         hash = ByteUtils.convertStringToBytes(s);
57     }
58
59     public void setPassword(String JavaDoc password) {
60         SHA256 sha = new SHA256();
61         String JavaDoc name = newName == null ? user.getName() : newName;
62         this.userPasswordHash = sha.getKeyPasswordHash(name, password.toCharArray());
63     }
64
65     public int update() throws SQLException JavaDoc {
66         session.commit();
67         Database db = session.getDatabase();
68         switch(type) {
69         case SET_PASSWORD:
70             if(user != session.getUser()) {
71                 session.getUser().checkAdmin();
72             }
73             if(hash!=null && salt !=null) {
74                 user.setSaltAndHash(salt, hash);
75             } else {
76                 user.setUserPasswordHash(userPasswordHash);
77             }
78             break;
79         case RENAME:
80             session.getUser().checkAdmin();
81             if(db.findUser(newName) != null || newName.equals(user.getName())) {
82                 throw Message.getSQLException(Message.USER_ALREADY_EXISTS_1, newName);
83             }
84             db.renameDatabaseObject(session, user, newName);
85             break;
86         case ADMIN:
87             session.getUser().checkAdmin();
88             user.setAdmin(admin);
89             break;
90         default:
91             throw Message.getInternalError("type="+type);
92         }
93         db.update(session, user);
94         return 0;
95     }
96
97 }
98
Popular Tags