James Moger
2012-04-25 e191104cd356faa2e261cc37585143878e23298d
commit | author | age
f13c4c 1 /*
JM 2  * Copyright 2011 gitblit.com.
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  */
1f9dae 16 package com.gitblit.models;
dfb889 17
JM 18 import java.io.Serializable;
8c9a20 19 import java.security.Principal;
831469 20 import java.util.HashSet;
JM 21 import java.util.Set;
dfb889 22
efe8ec 23 import com.gitblit.utils.StringUtils;
JM 24
d9f687 25 /**
JM 26  * UserModel is a serializable model class that represents a user and the user's
27  * restricted repository memberships. Instances of UserModels are also used as
28  * servlet user principals.
29  * 
30  * @author James Moger
31  * 
32  */
4b430b 33 public class UserModel implements Principal, Serializable, Comparable<UserModel> {
dfb889 34
JM 35     private static final long serialVersionUID = 1L;
36
2a7306 37     // field names are reflectively mapped in EditUser page
JM 38     public String username;
39     public String password;
7e0ce4 40     public String displayName;
JC 41     public String emailAddress;
2a7306 42     public boolean canAdmin;
831469 43     public boolean excludeFromFederation;
JM 44     public final Set<String> repositories = new HashSet<String>();
fe24a0 45     public final Set<TeamModel> teams = new HashSet<TeamModel>();
dfb889 46
511554 47     public UserModel(String username) {
dfb889 48         this.username = username;
JM 49     }
50
efe8ec 51     /**
JM 52      * This method does not take into consideration Ownership where the
53      * administrator has not explicitly granted access to the owner.
54      * 
55      * @param repositoryName
56      * @return
57      */
58     @Deprecated
d0d438 59     public boolean canAccessRepository(String repositoryName) {
fe24a0 60         return canAdmin || repositories.contains(repositoryName.toLowerCase())
JM 61                 || hasTeamAccess(repositoryName);
dfb889 62     }
JM 63
efe8ec 64     public boolean canAccessRepository(RepositoryModel repository) {
JM 65         boolean isOwner = !StringUtils.isEmpty(repository.owner)
66                 && repository.owner.equals(username);
fe24a0 67         return canAdmin || isOwner || repositories.contains(repository.name.toLowerCase())
JM 68                 || hasTeamAccess(repository.name);
69     }
70
71     public boolean hasTeamAccess(String repositoryName) {
72         for (TeamModel team : teams) {
73             if (team.hasRepository(repositoryName)) {
74                 return true;
75             }
76         }
77         return false;
efe8ec 78     }
JM 79
93f472 80     public boolean hasRepository(String name) {
JM 81         return repositories.contains(name.toLowerCase());
82     }
83
dfb889 84     public void addRepository(String name) {
JM 85         repositories.add(name.toLowerCase());
86     }
87
93f472 88     public void removeRepository(String name) {
JM 89         repositories.remove(name.toLowerCase());
90     }
91
fe24a0 92     public boolean isTeamMember(String teamname) {
JM 93         for (TeamModel team : teams) {
94             if (team.name.equalsIgnoreCase(teamname)) {
95                 return true;
96             }
97         }
98         return false;
99     }
100
997c16 101     public TeamModel getTeam(String teamname) {
JM 102         if (teams == null) {
103             return null;
104         }
105         for (TeamModel team : teams) {
106             if (team.name.equalsIgnoreCase(teamname)) {
107                 return team;
108             }
109         }
110         return null;
111     }
112
8a2e9c 113     @Override
85c2e6 114     public String getName() {
8c9a20 115         return username;
JM 116     }
d2426e 117     
JM 118     public String getDisplayName() {
119         if (StringUtils.isEmpty(displayName)) {
120             return username;
121         }
122         return displayName;
123     }
8c9a20 124
JM 125     @Override
dfb889 126     public String toString() {
JM 127         return username;
128     }
4b430b 129
JM 130     @Override
131     public int compareTo(UserModel o) {
132         return username.compareTo(o.username);
133     }
dfb889 134 }