1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/*
* Copyright 2011 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.DeepCopier;
/**
* This class wraps the default user service and is recommended as the starting
* point for custom user service implementations.
*
* This does seem a little convoluted, but the idea is to allow IUserService to
* evolve with new methods and implementations without breaking custom
* authentication implementations.
*
* The most common implementation of a custom IUserService is to only override
* authentication and then delegate all other functionality to one of Gitblit's
* user services. This class optimizes that use-case.
*
* Extending GitblitUserService allows for authentication customization without
* having to keep-up-with IUSerService API changes.
*
* @author James Moger
*
*/
public class GitblitUserService implements IUserService {
protected IUserService serviceImpl;
private final Logger logger = LoggerFactory.getLogger(GitblitUserService.class);
public GitblitUserService() {
}
@Override
public void setup(IStoredSettings settings) {
File realmFile = GitBlit.getFileOrFolder(Keys.realm.userService, "users.conf");
serviceImpl = createUserService(realmFile);
logger.info("GUS delegating to " + serviceImpl.toString());
}
@SuppressWarnings("deprecation")
protected IUserService createUserService(File realmFile) {
IUserService service = null;
if (realmFile.getName().toLowerCase().endsWith(".properties")) {
// v0.5.0 - v0.7.0 properties-based realm file
service = new FileUserService(realmFile);
} else if (realmFile.getName().toLowerCase().endsWith(".conf")) {
// v0.8.0+ config-based realm file
service = new ConfigUserService(realmFile);
}
assert service != null;
if (!realmFile.exists()) {
// Create the Administrator account for a new realm file
try {
realmFile.createNewFile();
} catch (IOException x) {
logger.error(MessageFormat.format("COULD NOT CREATE REALM FILE {0}!", realmFile), x);
}
UserModel admin = new UserModel("admin");
admin.password = "admin";
admin.canAdmin = true;
admin.excludeFromFederation = true;
service.updateUserModel(admin);
}
if (service instanceof FileUserService) {
// automatically create a users.conf realm file from the original
// users.properties file
File usersConfig = new File(realmFile.getParentFile(), "users.conf");
if (!usersConfig.exists()) {
logger.info(MessageFormat.format("Automatically creating {0} based on {1}",
usersConfig.getAbsolutePath(), realmFile.getAbsolutePath()));
ConfigUserService configService = new ConfigUserService(usersConfig);
for (String username : service.getAllUsernames()) {
UserModel userModel = service.getUserModel(username);
configService.updateUserModel(userModel);
}
}
// issue suggestion about switching to users.conf
logger.warn("Please consider using \"users.conf\" instead of the deprecated \"users.properties\" file");
}
return service;
}
@Override
public String toString() {
return getClass().getSimpleName();
}
@Override
public boolean supportsCredentialChanges() {
return serviceImpl.supportsCredentialChanges();
}
@Override
public boolean supportsTeamMembershipChanges() {
return serviceImpl.supportsTeamMembershipChanges();
}
@Override
public boolean supportsCookies() {
return serviceImpl.supportsCookies();
}
@Override
public char[] getCookie(UserModel model) {
return serviceImpl.getCookie(model);
}
@Override
public UserModel authenticate(char[] cookie) {
return serviceImpl.authenticate(cookie);
}
@Override
public UserModel authenticate(String username, char[] password) {
return serviceImpl.authenticate(username, password);
}
@Override
public UserModel getUserModel(String username) {
return serviceImpl.getUserModel(username);
}
@Override
public boolean updateUserModel(UserModel model) {
return serviceImpl.updateUserModel(model);
}
@Override
public boolean updateUserModel(String username, UserModel model) {
if (supportsCredentialChanges()) {
if (!supportsTeamMembershipChanges()) {
// teams are externally controlled
model = DeepCopier.copy(model);
model.teams.clear();
}
return serviceImpl.updateUserModel(username, model);
}
if (model.username.equals(username)) {
// passwords are not persisted by the backing user service
model.password = null;
if (!supportsTeamMembershipChanges()) {
// teams are externally controlled
model = DeepCopier.copy(model);
model.teams.clear();
}
return serviceImpl.updateUserModel(username, model);
}
logger.error("Users can not be renamed!");
return false;
}
@Override
public boolean deleteUserModel(UserModel model) {
return serviceImpl.deleteUserModel(model);
}
@Override
public boolean deleteUser(String username) {
return serviceImpl.deleteUser(username);
}
@Override
public List<String> getAllUsernames() {
return serviceImpl.getAllUsernames();
}
@Override
public List<UserModel> getAllUsers() {
return serviceImpl.getAllUsers();
}
@Override
public List<String> getAllTeamNames() {
return serviceImpl.getAllTeamNames();
}
@Override
public List<TeamModel> getAllTeams() {
return serviceImpl.getAllTeams();
}
@Override
public List<String> getTeamnamesForRepositoryRole(String role) {
return serviceImpl.getTeamnamesForRepositoryRole(role);
}
@Override
public boolean setTeamnamesForRepositoryRole(String role, List<String> teamnames) {
return serviceImpl.setTeamnamesForRepositoryRole(role, teamnames);
}
@Override
public TeamModel getTeamModel(String teamname) {
return serviceImpl.getTeamModel(teamname);
}
@Override
public boolean updateTeamModel(TeamModel model) {
return serviceImpl.updateTeamModel(model);
}
@Override
public boolean updateTeamModel(String teamname, TeamModel model) {
if (!supportsTeamMembershipChanges()) {
// teams are externally controlled
model = DeepCopier.copy(model);
model.users.clear();
}
return serviceImpl.updateTeamModel(teamname, model);
}
@Override
public boolean deleteTeamModel(TeamModel model) {
return serviceImpl.deleteTeamModel(model);
}
@Override
public boolean deleteTeam(String teamname) {
return serviceImpl.deleteTeam(teamname);
}
@Override
public List<String> getUsernamesForRepositoryRole(String role) {
return serviceImpl.getUsernamesForRepositoryRole(role);
}
@Override
public boolean setUsernamesForRepositoryRole(String role, List<String> usernames) {
return serviceImpl.setUsernamesForRepositoryRole(role, usernames);
}
@Override
public boolean renameRepositoryRole(String oldRole, String newRole) {
return serviceImpl.renameRepositoryRole(oldRole, newRole);
}
@Override
public boolean deleteRepositoryRole(String role) {
return serviceImpl.deleteRepositoryRole(role);
}
}
|