KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > authentication > UserCreator


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.authentication;
17
18 import org.outerj.daisy.repository.user.User;
19 import org.outerj.daisy.repository.user.UserManager;
20 import org.outerj.daisy.repository.user.Role;
21 import org.outerj.daisy.repository.RepositoryException;
22 import org.outerj.daisy.authentication.AuthenticationException;
23
24 public class UserCreator {
25     private final String JavaDoc defaultRole;
26     private final String JavaDoc[] roles;
27     private final boolean updateableByUser;
28     private final String JavaDoc scheme;
29
30     public UserCreator(String JavaDoc[] roles, String JavaDoc defaultRole, boolean updateableByUser, String JavaDoc scheme) {
31         this.roles = roles;
32         this.defaultRole = defaultRole;
33         this.updateableByUser = updateableByUser;
34         this.scheme = scheme;
35     }
36
37     public User create(String JavaDoc login, UserManager userManager) throws AuthenticationException {
38         User user = userManager.createUser(login);
39         try {
40             for (int i = 0; i < roles.length; i++) {
41                 Role role = userManager.getRole(roles[i], false);
42                 user.addToRole(role);
43             }
44
45             if (defaultRole != null)
46                 user.setDefaultRole(userManager.getRole(defaultRole, false));
47
48             user.setUpdateableByUser(updateableByUser);
49             user.setAuthenticationScheme(scheme);
50
51             user.save();
52         } catch (RepositoryException e) {
53             throw new AuthenticationException("Error creating new user during login.", e);
54         }
55
56         return user;
57     }
58 }
59
Popular Tags