KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class CreateUser extends DefineCommand {
17
18     private String JavaDoc userName;
19     private boolean admin;
20     private byte[] userPasswordHash;
21     private byte[] salt;
22     private byte[] hash;
23     private boolean ifNotExists;
24     private String JavaDoc comment;
25
26     public CreateUser(Session session) {
27         super(session);
28     }
29
30     public void setIfNotExists(boolean ifNotExists) {
31         this.ifNotExists = ifNotExists;
32     }
33
34     public void setUserName(String JavaDoc userName) {
35         this.userName = userName;
36     }
37
38     public void setPassword(String JavaDoc password) {
39         SHA256 sha = new SHA256();
40         this.userPasswordHash = sha.getKeyPasswordHash(userName, password.toCharArray());
41     }
42
43     public int update() throws SQLException JavaDoc {
44         session.getUser().checkAdmin();
45         session.commit();
46         Database db = session.getDatabase();
47         if(db.findUser(userName)!=null) {
48             if (ifNotExists) {
49                 return 0;
50             }
51             throw Message.getSQLException(Message.USER_ALREADY_EXISTS_1, userName);
52         }
53         int id = getObjectId(false, true);
54         User user = new User(db, id, userName, false);
55         user.setAdmin(admin);
56         user.setComment(comment);
57         if(hash!=null && salt !=null) {
58             user.setSaltAndHash(salt, hash);
59         } else {
60             user.setUserPasswordHash(userPasswordHash);
61         }
62         db.addDatabaseObject(session, user);
63         return 0;
64     }
65
66     public void setSalt(String JavaDoc s) throws SQLException JavaDoc {
67         salt = ByteUtils.convertStringToBytes(s);
68     }
69
70     public void setHash(String JavaDoc s) throws SQLException JavaDoc {
71         hash = ByteUtils.convertStringToBytes(s);
72     }
73
74     public void setAdmin(boolean b) {
75         admin = b;
76     }
77
78     public void setComment(String JavaDoc comment) {
79         this.comment = comment;
80     }
81
82 }
83
Popular Tags