James Moger
2012-07-11 a31cf95ac0787eb559cb78f48c52bc6b79e970d8
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;
a31cf9 40     public String cookie;
7e0ce4 41     public String displayName;
JC 42     public String emailAddress;
2a7306 43     public boolean canAdmin;
831469 44     public boolean excludeFromFederation;
JM 45     public final Set<String> repositories = new HashSet<String>();
fe24a0 46     public final Set<TeamModel> teams = new HashSet<TeamModel>();
dfb889 47
511554 48     public UserModel(String username) {
dfb889 49         this.username = username;
JM 50     }
51
efe8ec 52     /**
JM 53      * This method does not take into consideration Ownership where the
54      * administrator has not explicitly granted access to the owner.
55      * 
56      * @param repositoryName
57      * @return
58      */
59     @Deprecated
d0d438 60     public boolean canAccessRepository(String repositoryName) {
fe24a0 61         return canAdmin || repositories.contains(repositoryName.toLowerCase())
JM 62                 || hasTeamAccess(repositoryName);
dfb889 63     }
JM 64
efe8ec 65     public boolean canAccessRepository(RepositoryModel repository) {
JM 66         boolean isOwner = !StringUtils.isEmpty(repository.owner)
67                 && repository.owner.equals(username);
fe24a0 68         return canAdmin || isOwner || repositories.contains(repository.name.toLowerCase())
JM 69                 || hasTeamAccess(repository.name);
70     }
71
72     public boolean hasTeamAccess(String repositoryName) {
73         for (TeamModel team : teams) {
74             if (team.hasRepository(repositoryName)) {
75                 return true;
76             }
77         }
78         return false;
efe8ec 79     }
JM 80
93f472 81     public boolean hasRepository(String name) {
JM 82         return repositories.contains(name.toLowerCase());
83     }
84
dfb889 85     public void addRepository(String name) {
JM 86         repositories.add(name.toLowerCase());
87     }
88
93f472 89     public void removeRepository(String name) {
JM 90         repositories.remove(name.toLowerCase());
91     }
92
fe24a0 93     public boolean isTeamMember(String teamname) {
JM 94         for (TeamModel team : teams) {
95             if (team.name.equalsIgnoreCase(teamname)) {
96                 return true;
97             }
98         }
99         return false;
100     }
101
997c16 102     public TeamModel getTeam(String teamname) {
JM 103         if (teams == null) {
104             return null;
105         }
106         for (TeamModel team : teams) {
107             if (team.name.equalsIgnoreCase(teamname)) {
108                 return team;
109             }
110         }
111         return null;
112     }
113
8a2e9c 114     @Override
85c2e6 115     public String getName() {
8c9a20 116         return username;
JM 117     }
d2426e 118     
JM 119     public String getDisplayName() {
120         if (StringUtils.isEmpty(displayName)) {
121             return username;
122         }
123         return displayName;
124     }
8c9a20 125
JM 126     @Override
dfb889 127     public String toString() {
JM 128         return username;
129     }
4b430b 130
JM 131     @Override
132     public int compareTo(UserModel o) {
133         return username.compareTo(o.username);
134     }
dfb889 135 }