001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package web.security.impl;
017:
018: import java.io.Serializable;
019: import java.util.List;
020: import java.util.Vector;
021:
022: import web.security.Group;
023: import web.security.Privilege;
024: import web.security.Role;
025: import web.security.User;
026:
027: /**
028: * 用户的实现类
029: * @author liudong
030: */
031: public class UserImpl implements User, Serializable {
032:
033: String name;
034: String password;
035: List roles;
036: List groups;
037:
038: public UserImpl() {
039: this (null);
040: }
041:
042: public UserImpl(String name) {
043: this .name = name;
044: }
045:
046: /* (non-Javadoc)
047: * @see com.clickcom.web.security.User#groups()
048: */
049: public Group[] groups() {
050: if (groups == null)
051: return null;
052: return (Group[]) groups.toArray(new Group[groups.size()]);
053: }
054:
055: /* (non-Javadoc)
056: * @see com.clickcom.web.security.Actor#getName()
057: */
058: public String getName() {
059: return name;
060: }
061:
062: /* (non-Javadoc)
063: * @see com.clickcom.web.security.Actor#roles()
064: */
065: public Role[] roles() {
066: if (roles == null)
067: return null;
068: return (Role[]) roles.toArray(new Role[roles.size()]);
069: }
070:
071: /**
072: * 增加用户角色
073: * @param role
074: */
075: public void addRole(Role role) {
076: if (roles == null)
077: roles = new Vector();
078: if (!roles.contains(role))
079: roles.add(role);
080: }
081:
082: /**
083: * 判断用户所属的角色是否允许访问,否则再判断用户所在的组所属的角色可否访问
084: * @see web.security.Actor#canDo(com.clickcom.web.security.Privilege)
085: */
086: public boolean canDo(Privilege pvg) {
087: for (int i = 0; roles != null && i < roles.size(); i++) {
088: if (((Role) roles.get(i)).canDo(pvg))
089: return true;
090: }
091: for (int i = 0; groups != null && i < groups.size(); i++) {
092: Group g = (Group) groups.get(i);
093: Role[] rs = g.roles();
094: for (int j = 0; rs != null && j < rs.length; j++) {
095: if (rs[j].canDo(pvg))
096: return true;
097: }
098: }
099: return false;
100: }
101:
102: public void setGroups(List groups) {
103: this .groups = groups;
104: }
105:
106: public void setName(String name) {
107: this .name = name;
108: }
109:
110: public void setRoles(List roles) {
111: this .roles = roles;
112: }
113:
114: public List getGroups() {
115: return groups;
116: }
117:
118: public List getRoles() {
119: return roles;
120: }
121:
122: public String getPassword() {
123: return password;
124: }
125:
126: public void setPassword(String password) {
127: this.password = password;
128: }
129: }
|