KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > web > security > impl > UserImpl


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package web.security.impl;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import web.security.Group;
23 import web.security.Privilege;
24 import web.security.Role;
25 import web.security.User;
26
27
28 /**
29  * 用户的实现类
30  * @author liudong
31  */

32 public class UserImpl implements User, Serializable JavaDoc {
33     
34     String JavaDoc name;
35     String JavaDoc password;
36     List JavaDoc roles;
37     List JavaDoc groups;
38
39     public UserImpl(){
40         this(null);
41     }
42     
43     public UserImpl(String JavaDoc name){
44         this.name = name;
45     }
46     
47     /* (non-Javadoc)
48      * @see com.clickcom.web.security.User#groups()
49      */

50     public Group[] groups() {
51         if(groups == null)
52             return null;
53         return (Group[])groups.toArray(new Group[groups.size()]);
54     }
55
56     /* (non-Javadoc)
57      * @see com.clickcom.web.security.Actor#getName()
58      */

59     public String JavaDoc getName() {
60         return name;
61     }
62
63     /* (non-Javadoc)
64      * @see com.clickcom.web.security.Actor#roles()
65      */

66     public Role[] roles() {
67         if(roles == null)
68             return null;
69         return (Role[])roles.toArray(new Role[roles.size()]);
70     }
71
72     /**
73      * 增加用户角色
74      * @param role
75      */

76     public void addRole(Role role) {
77         if(roles==null)
78             roles = new Vector JavaDoc();
79         if(!roles.contains(role))
80             roles.add(role);
81     }
82     /**
83      * 判断用户所属的角色是否允许访问,否则再判断用户所在的组所属的角色可否访问
84      * @see web.security.Actor#canDo(com.clickcom.web.security.Privilege)
85      */

86     public boolean canDo(Privilege pvg) {
87         for(int i=0;roles!=null&&i<roles.size();i++){
88             if(((Role)roles.get(i)).canDo(pvg))
89                 return true;
90         }
91         for(int i=0;groups!=null&&i<groups.size();i++){
92             Group g = (Group)groups.get(i);
93             Role[] rs = g.roles();
94             for(int j=0;rs!=null&&j<rs.length;j++){
95                 if(rs[j].canDo(pvg))
96                     return true;
97             }
98         }
99         return false;
100     }
101
102     public void setGroups(List JavaDoc groups) {
103         this.groups = groups;
104     }
105     public void setName(String JavaDoc name) {
106         this.name = name;
107     }
108     public void setRoles(List JavaDoc roles) {
109         this.roles = roles;
110     }
111     public List JavaDoc getGroups() {
112         return groups;
113     }
114     public List JavaDoc getRoles() {
115         return roles;
116     }
117
118     public String JavaDoc getPassword() {
119         return password;
120     }
121     public void setPassword(String JavaDoc password) {
122         this.password = password;
123     }
124 }
125
Popular Tags