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