commit | author | age
|
a7571b
|
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 |
*/
|
|
16 |
package com.gitblit.wicket.pages;
|
|
17 |
|
|
18 |
import java.text.MessageFormat;
|
9e5bee
|
19 |
import java.util.ArrayList;
|
31bcbe
|
20 |
import java.util.LinkedHashSet;
|
9e5bee
|
21 |
import java.util.List;
|
31bcbe
|
22 |
import java.util.Set;
|
cb57ec
|
23 |
import java.util.regex.Pattern;
|
a7571b
|
24 |
|
d376ab
|
25 |
import org.apache.wicket.PageParameters;
|
a7571b
|
26 |
import org.apache.wicket.markup.html.form.PasswordTextField;
|
JM |
27 |
import org.apache.wicket.markup.html.form.StatelessForm;
|
|
28 |
import org.apache.wicket.markup.html.form.TextField;
|
|
29 |
import org.apache.wicket.model.IModel;
|
|
30 |
import org.apache.wicket.model.Model;
|
|
31 |
import org.apache.wicket.protocol.http.WebResponse;
|
|
32 |
|
|
33 |
import com.gitblit.Constants;
|
|
34 |
import com.gitblit.GitBlit;
|
|
35 |
import com.gitblit.Keys;
|
cb57ec
|
36 |
import com.gitblit.models.RepositoryModel;
|
JM |
37 |
import com.gitblit.models.TeamModel;
|
a7571b
|
38 |
import com.gitblit.models.UserModel;
|
JM |
39 |
import com.gitblit.utils.StringUtils;
|
|
40 |
import com.gitblit.wicket.GitBlitWebSession;
|
9e5bee
|
41 |
import com.gitblit.wicket.PageRegistration;
|
31bcbe
|
42 |
import com.gitblit.wicket.PageRegistration.DropDownMenuItem;
|
8c5d72
|
43 |
import com.gitblit.wicket.WicketUtils;
|
9e5bee
|
44 |
import com.gitblit.wicket.panels.NavigationPanel;
|
a7571b
|
45 |
|
d376ab
|
46 |
/**
|
JM |
47 |
* Root page is a topbar, navigable page like Repositories, Users, or
|
|
48 |
* Federation.
|
|
49 |
*
|
|
50 |
* @author James Moger
|
|
51 |
*
|
|
52 |
*/
|
a7571b
|
53 |
public abstract class RootPage extends BasePage {
|
JM |
54 |
|
d376ab
|
55 |
boolean showAdmin;
|
a7571b
|
56 |
|
JM |
57 |
IModel<String> username = new Model<String>("");
|
|
58 |
IModel<String> password = new Model<String>("");
|
|
59 |
|
|
60 |
public RootPage() {
|
|
61 |
super();
|
d376ab
|
62 |
}
|
a7571b
|
63 |
|
d376ab
|
64 |
public RootPage(PageParameters params) {
|
JM |
65 |
super(params);
|
|
66 |
}
|
a7571b
|
67 |
|
d376ab
|
68 |
@Override
|
JM |
69 |
protected void setupPage(String repositoryName, String pageName) {
|
31bcbe
|
70 |
boolean authenticateView = GitBlit.getBoolean(Keys.web.authenticateViewPages, false);
|
JM |
71 |
boolean authenticateAdmin = GitBlit.getBoolean(Keys.web.authenticateAdminPages, true);
|
|
72 |
boolean allowAdmin = GitBlit.getBoolean(Keys.web.allowAdministration, true);
|
|
73 |
|
|
74 |
if (authenticateAdmin) {
|
a7571b
|
75 |
showAdmin = allowAdmin && GitBlitWebSession.get().canAdmin();
|
JM |
76 |
// authentication requires state and session
|
|
77 |
setStatelessHint(false);
|
|
78 |
} else {
|
31bcbe
|
79 |
showAdmin = allowAdmin;
|
JM |
80 |
if (authenticateView) {
|
a7571b
|
81 |
// authentication requires state and session
|
JM |
82 |
setStatelessHint(false);
|
|
83 |
} else {
|
|
84 |
// no authentication required, no state and no session required
|
|
85 |
setStatelessHint(true);
|
|
86 |
}
|
|
87 |
}
|
|
88 |
boolean showRegistrations = GitBlit.canFederate()
|
|
89 |
&& GitBlit.getBoolean(Keys.web.showFederationRegistrations, false);
|
|
90 |
|
|
91 |
// navigation links
|
9e5bee
|
92 |
List<PageRegistration> pages = new ArrayList<PageRegistration>();
|
JM |
93 |
pages.add(new PageRegistration("gb.repositories", RepositoriesPage.class));
|
6e6f9f
|
94 |
pages.add(new PageRegistration("gb.activity", ActivityPage.class));
|
9e5bee
|
95 |
if (showAdmin) {
|
JM |
96 |
pages.add(new PageRegistration("gb.users", UsersPage.class));
|
|
97 |
}
|
|
98 |
if (showAdmin || showRegistrations) {
|
|
99 |
pages.add(new PageRegistration("gb.federation", FederationPage.class));
|
cb57ec
|
100 |
}
|
31bcbe
|
101 |
|
JM |
102 |
if (!authenticateView || (authenticateView && GitBlitWebSession.get().isLoggedIn())) {
|
|
103 |
addDropDownMenus(pages);
|
|
104 |
}
|
|
105 |
|
9e5bee
|
106 |
NavigationPanel navPanel = new NavigationPanel("navPanel", getClass(), pages);
|
JM |
107 |
add(navPanel);
|
a7571b
|
108 |
|
JM |
109 |
// login form
|
|
110 |
StatelessForm<Void> loginForm = new StatelessForm<Void>("loginForm") {
|
|
111 |
|
|
112 |
private static final long serialVersionUID = 1L;
|
|
113 |
|
|
114 |
@Override
|
|
115 |
public void onSubmit() {
|
|
116 |
String username = RootPage.this.username.getObject();
|
|
117 |
char[] password = RootPage.this.password.getObject().toCharArray();
|
|
118 |
|
|
119 |
UserModel user = GitBlit.self().authenticate(username, password);
|
|
120 |
if (user == null) {
|
|
121 |
error("Invalid username or password!");
|
|
122 |
} else if (user.username.equals(Constants.FEDERATION_USER)) {
|
|
123 |
// disallow the federation user from logging in via the
|
|
124 |
// web ui
|
|
125 |
error("Invalid username or password!");
|
|
126 |
user = null;
|
|
127 |
} else {
|
|
128 |
loginUser(user);
|
|
129 |
}
|
|
130 |
}
|
|
131 |
};
|
8c5d72
|
132 |
TextField<String> unameField = new TextField<String>("username", username);
|
JM |
133 |
WicketUtils.setInputPlaceholder(unameField, getString("gb.username"));
|
|
134 |
loginForm.add(unameField);
|
|
135 |
PasswordTextField pwField = new PasswordTextField("password", password);
|
|
136 |
WicketUtils.setInputPlaceholder(pwField, getString("gb.password"));
|
|
137 |
loginForm.add(pwField);
|
a7571b
|
138 |
add(loginForm);
|
31bcbe
|
139 |
|
JM |
140 |
if (authenticateView || authenticateAdmin) {
|
a7571b
|
141 |
loginForm.setVisible(!GitBlitWebSession.get().isLoggedIn());
|
JM |
142 |
} else {
|
|
143 |
loginForm.setVisible(false);
|
|
144 |
}
|
|
145 |
|
|
146 |
// display an error message cached from a redirect
|
|
147 |
String cachedMessage = GitBlitWebSession.get().clearErrorMessage();
|
|
148 |
if (!StringUtils.isEmpty(cachedMessage)) {
|
|
149 |
error(cachedMessage);
|
|
150 |
} else if (showAdmin) {
|
|
151 |
int pendingProposals = GitBlit.self().getPendingFederationProposals().size();
|
|
152 |
if (pendingProposals == 1) {
|
|
153 |
info("There is 1 federation proposal awaiting review.");
|
|
154 |
} else if (pendingProposals > 1) {
|
|
155 |
info(MessageFormat.format("There are {0} federation proposals awaiting review.",
|
|
156 |
pendingProposals));
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
d376ab
|
160 |
super.setupPage(repositoryName, pageName);
|
a7571b
|
161 |
}
|
JM |
162 |
|
|
163 |
private void loginUser(UserModel user) {
|
|
164 |
if (user != null) {
|
|
165 |
// Set the user into the session
|
|
166 |
GitBlitWebSession.get().setUser(user);
|
|
167 |
|
|
168 |
// Set Cookie
|
|
169 |
if (GitBlit.getBoolean(Keys.web.allowCookieAuthentication, false)) {
|
|
170 |
WebResponse response = (WebResponse) getRequestCycle().getResponse();
|
|
171 |
GitBlit.self().setCookie(response, user);
|
|
172 |
}
|
|
173 |
|
|
174 |
if (!continueToOriginalDestination()) {
|
|
175 |
// Redirect to home page
|
|
176 |
setResponsePage(getApplication().getHomePage());
|
|
177 |
}
|
|
178 |
}
|
31bcbe
|
179 |
}
|
JM |
180 |
|
|
181 |
protected void addDropDownMenus(List<PageRegistration> pages) {
|
|
182 |
|
|
183 |
}
|
|
184 |
|
|
185 |
protected List<DropDownMenuItem> getFilterMenuItems() {
|
|
186 |
final UserModel user = GitBlitWebSession.get().getUser();
|
|
187 |
Set<DropDownMenuItem> filters = new LinkedHashSet<DropDownMenuItem>();
|
|
188 |
|
|
189 |
// accessible repositories by federation set
|
|
190 |
for (RepositoryModel repository : GitBlit.self().getRepositoryModels(user)) {
|
|
191 |
for (String set : repository.federationSets) {
|
|
192 |
filters.add(new DropDownMenuItem(set, "set", set));
|
|
193 |
}
|
|
194 |
}
|
|
195 |
if (filters.size() > 0) {
|
|
196 |
// divider
|
|
197 |
filters.add(new DropDownMenuItem());
|
|
198 |
}
|
|
199 |
|
|
200 |
// user's team memberships
|
|
201 |
if (user != null && user.teams.size() > 0) {
|
|
202 |
for (TeamModel team : user.teams) {
|
|
203 |
filters.add(new DropDownMenuItem(team.name, "team", team.name));
|
|
204 |
}
|
|
205 |
// divider
|
|
206 |
filters.add(new DropDownMenuItem());
|
|
207 |
}
|
|
208 |
|
|
209 |
// custom filters
|
|
210 |
String customFilters = GitBlit.getString(Keys.web.customFilters, null);
|
|
211 |
if (!StringUtils.isEmpty(customFilters)) {
|
|
212 |
List<String> expressions = StringUtils.getStringsFromValue(customFilters, "!!!");
|
|
213 |
for (String expression : expressions) {
|
|
214 |
filters.add(new DropDownMenuItem(null, "x", expression));
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
if (filters.size() > 0) {
|
|
219 |
// if we have any filters, add the divider
|
|
220 |
filters.add(new DropDownMenuItem());
|
|
221 |
// add All Repositories
|
|
222 |
filters.add(new DropDownMenuItem("All Repositories", null, null));
|
|
223 |
}
|
|
224 |
|
|
225 |
return new ArrayList<DropDownMenuItem>(filters);
|
a7571b
|
226 |
}
|
cb57ec
|
227 |
|
JM |
228 |
protected List<RepositoryModel> getRepositories(PageParameters params) {
|
|
229 |
final UserModel user = GitBlitWebSession.get().getUser();
|
|
230 |
if (params == null) {
|
|
231 |
return GitBlit.self().getRepositoryModels(user);
|
|
232 |
}
|
|
233 |
|
|
234 |
String repositoryName = WicketUtils.getRepositoryName(params);
|
|
235 |
String set = WicketUtils.getSet(params);
|
|
236 |
String regex = WicketUtils.getRegEx(params);
|
|
237 |
String team = WicketUtils.getTeam(params);
|
|
238 |
|
|
239 |
List<RepositoryModel> models = null;
|
|
240 |
|
|
241 |
if (!StringUtils.isEmpty(repositoryName)) {
|
|
242 |
// try named repository
|
|
243 |
models = new ArrayList<RepositoryModel>();
|
|
244 |
RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
|
|
245 |
if (user.canAccessRepository(model)) {
|
|
246 |
models.add(model);
|
|
247 |
}
|
|
248 |
}
|
|
249 |
|
|
250 |
// get all user accessible repositories
|
|
251 |
if (models == null) {
|
|
252 |
models = GitBlit.self().getRepositoryModels(user);
|
|
253 |
}
|
|
254 |
|
|
255 |
if (!StringUtils.isEmpty(regex)) {
|
|
256 |
// filter the repositories by the regex
|
|
257 |
List<RepositoryModel> accessible = GitBlit.self().getRepositoryModels(user);
|
|
258 |
List<RepositoryModel> matchingModels = new ArrayList<RepositoryModel>();
|
|
259 |
Pattern pattern = Pattern.compile(regex);
|
|
260 |
for (RepositoryModel aModel : accessible) {
|
|
261 |
if (pattern.matcher(aModel.name).find()) {
|
|
262 |
matchingModels.add(aModel);
|
|
263 |
}
|
|
264 |
}
|
|
265 |
models = matchingModels;
|
|
266 |
} else if (!StringUtils.isEmpty(set)) {
|
|
267 |
// filter the repositories by the specified sets
|
|
268 |
List<String> sets = StringUtils.getStringsFromValue(set, ",");
|
|
269 |
List<RepositoryModel> matchingModels = new ArrayList<RepositoryModel>();
|
|
270 |
for (RepositoryModel model : models) {
|
|
271 |
for (String curr : sets) {
|
|
272 |
if (model.federationSets.contains(curr)) {
|
|
273 |
matchingModels.add(model);
|
|
274 |
}
|
|
275 |
}
|
|
276 |
}
|
|
277 |
models = matchingModels;
|
|
278 |
} else if (!StringUtils.isEmpty(team)) {
|
|
279 |
// filter the repositories by the specified teams
|
|
280 |
List<String> teams = StringUtils.getStringsFromValue(team, ",");
|
31bcbe
|
281 |
|
cb57ec
|
282 |
// need TeamModels first
|
JM |
283 |
List<TeamModel> teamModels = new ArrayList<TeamModel>();
|
|
284 |
for (String name : teams) {
|
|
285 |
TeamModel model = GitBlit.self().getTeamModel(name);
|
|
286 |
if (model != null) {
|
|
287 |
teamModels.add(model);
|
|
288 |
}
|
|
289 |
}
|
31bcbe
|
290 |
|
cb57ec
|
291 |
// brute-force our way through finding the matching models
|
JM |
292 |
List<RepositoryModel> matchingModels = new ArrayList<RepositoryModel>();
|
|
293 |
for (RepositoryModel repositoryModel : models) {
|
|
294 |
for (TeamModel teamModel : teamModels) {
|
|
295 |
if (teamModel.hasRepository(repositoryModel.name)) {
|
|
296 |
matchingModels.add(repositoryModel);
|
|
297 |
}
|
|
298 |
}
|
|
299 |
}
|
|
300 |
models = matchingModels;
|
|
301 |
}
|
|
302 |
return models;
|
|
303 |
}
|
a7571b
|
304 |
}
|