James Moger
2014-07-03 efdb2b3d0c6f03a9aac9e65892cbc8ff755f246f
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  */
5fe7df 16 package com.gitblit.wicket;
JM 17
d0f6f2 18 import java.util.Locale;
d97e52 19 import java.util.Map;
5fe7df 20 import java.util.TimeZone;
1f52c8 21 import java.util.concurrent.atomic.AtomicBoolean;
5fe7df 22
d97e52 23 import org.apache.wicket.Page;
JM 24 import org.apache.wicket.PageParameters;
25 import org.apache.wicket.RedirectToUrlException;
5fe7df 26 import org.apache.wicket.Request;
JM 27 import org.apache.wicket.Session;
d97e52 28 import org.apache.wicket.protocol.http.RequestUtils;
JM 29 import org.apache.wicket.protocol.http.WebRequestCycle;
5fe7df 30 import org.apache.wicket.protocol.http.WebSession;
JM 31 import org.apache.wicket.protocol.http.request.WebClientInfo;
32
1f9dae 33 import com.gitblit.models.UserModel;
dfb889 34
5fe7df 35 public final class GitBlitWebSession extends WebSession {
JM 36
37     private static final long serialVersionUID = 1L;
38
2a7306 39     protected TimeZone timezone;
155bf7 40
2a7306 41     private UserModel user;
JM 42
43     private String errorMessage;
699e71 44
d97e52 45     private String requestUrl;
699e71 46
1f52c8 47     private AtomicBoolean isForking;
699e71 48
5fe7df 49     public GitBlitWebSession(Request request) {
JM 50         super(request);
1f52c8 51         isForking = new AtomicBoolean();
5fe7df 52     }
JM 53
699e71 54     @Override
5fe7df 55     public void invalidate() {
JM 56         super.invalidate();
94b96b 57         user = null;
d97e52 58     }
699e71 59
d97e52 60     /**
JM 61      * Cache the requested protected resource pending successful authentication.
699e71 62      *
d97e52 63      * @param pageClass
JM 64      */
65     public void cacheRequest(Class<? extends Page> pageClass) {
66         // build absolute url with correctly encoded parameters?!
67         Request req = WebRequestCycle.get().getRequest();
68         Map<String, ?> params = req.getRequestParameters().getParameters();
69         PageParameters pageParams = new PageParameters(params);
70         String relativeUrl = WebRequestCycle.get().urlFor(pageClass, pageParams).toString();
71         requestUrl = RequestUtils.toAbsolutePath(relativeUrl);
72         if (isTemporary())
73         {
74             // we must bind the temporary session into the session store
75             // so that we can re-use this session for reporting an error message
76             // on the redirected page and continuing the request after
77             // authentication.
78             bind();
79         }
80     }
699e71 81
d97e52 82     /**
JM 83      * Continue any cached request.  This is used when a request for a protected
84      * resource is aborted/redirected pending proper authentication.  Gitblit
85      * no longer uses Wicket's built-in mechanism for this because of Wicket's
86      * failure to properly handle parameters with forward-slashes.  This is a
87      * constant source of headaches with Wicket.
699e71 88      *
d97e52 89      * @return false if there is no cached request to process
JM 90      */
91     public boolean continueRequest() {
92         if (requestUrl != null) {
93             String url = requestUrl;
94             requestUrl = null;
95             throw new RedirectToUrlException(url);
96         }
97         return false;
94b96b 98     }
155bf7 99
94b96b 100     public boolean isLoggedIn() {
JM 101         return user != null;
102     }
155bf7 103
fc948c 104     public boolean canAdmin() {
JM 105         if (user == null) {
155bf7 106             return false;
fc948c 107         }
7f7051 108         return user.canAdmin();
fc948c 109     }
699e71 110
d97e52 111     public String getUsername() {
JM 112         return user == null ? "anonymous" : user.username;
113     }
155bf7 114
511554 115     public UserModel getUser() {
94b96b 116         return user;
JM 117     }
155bf7 118
511554 119     public void setUser(UserModel user) {
94b96b 120         this.user = user;
d0f6f2 121         if (user != null) {
JM 122             Locale preferredLocale = user.getPreferences().getLocale();
123             if (preferredLocale != null) {
124                 // set the user's preferred locale
125                 setLocale(preferredLocale);
126             }
127         }
5fe7df 128     }
JM 129
130     public TimeZone getTimezone() {
131         if (timezone == null) {
132             timezone = ((WebClientInfo) getClientInfo()).getProperties().getTimeZone();
133         }
134         // use server timezone if we can't determine the client timezone
135         if (timezone == null) {
136             timezone = TimeZone.getDefault();
137         }
138         return timezone;
139     }
2a7306 140
bc9d4a 141     public void cacheErrorMessage(String message) {
JM 142         this.errorMessage = message;
143     }
2a7306 144
bc9d4a 145     public String clearErrorMessage() {
JM 146         String msg = errorMessage;
147         errorMessage = null;
148         return msg;
149     }
699e71 150
1f52c8 151     public boolean isForking() {
JM 152         return isForking.get();
153     }
699e71 154
1f52c8 155     public void isForking(boolean val) {
JM 156         isForking.set(val);
157     }
5fe7df 158
JM 159     public static GitBlitWebSession get() {
160         return (GitBlitWebSession) Session.get();
161     }
162 }