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