KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > engine > common > service > impl > AcegiSecurityContextProvider


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.api.engine.common.service.impl;
22
23 import org.acegisecurity.Authentication;
24 import org.acegisecurity.context.SecurityContextHolder;
25 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
26 import org.acegisecurity.userdetails.UserDetails;
27 import org.acegisecurity.userdetails.UserDetailsService;
28
29 import com.jaspersoft.jasperserver.api.JSException;
30 import com.jaspersoft.jasperserver.api.engine.common.service.SecurityContextProvider;
31 import com.jaspersoft.jasperserver.api.metadata.user.domain.User;
32 import com.jaspersoft.jasperserver.api.metadata.user.service.UserAuthorityService;
33
34 /**
35  * @author Lucian Chirita (lucianc@users.sourceforge.net)
36  * @version $Id: AcegiSecurityContextProvider.java 3970 2006-07-13 13:28:20Z swood $
37  */

38 public class AcegiSecurityContextProvider implements SecurityContextProvider {
39     
40     private UserDetailsService userDetailsService;
41     private UserAuthorityService userAuthorityService;
42
43     public UserDetailsService getUserDetailsService() {
44         return userDetailsService;
45     }
46
47     public void setUserDetailsService(UserDetailsService userDetailsService) {
48         this.userDetailsService = userDetailsService;
49     }
50
51     public UserAuthorityService getUserAuthorityService() {
52         return userAuthorityService;
53     }
54
55     public void setUserAuthorityService(UserAuthorityService userAuthorityService) {
56         this.userAuthorityService = userAuthorityService;
57     }
58
59     public String JavaDoc getContextUsername() {
60         Authentication authenticationToken = SecurityContextHolder.getContext().getAuthentication();
61         if (authenticationToken == null) {
62             return null;
63         }
64         
65         if (authenticationToken.getPrincipal() instanceof UserDetails) {
66             UserDetails contextUserDetails = (UserDetails) authenticationToken.getPrincipal();
67             return contextUserDetails.getUsername();
68         } else if (authenticationToken.getPrincipal() instanceof String JavaDoc) {
69             return (String JavaDoc) authenticationToken.getPrincipal();
70         } else {
71             return null;
72         }
73     }
74     
75     public User getContextUser() {
76         String JavaDoc username = getContextUsername();
77         if (username == null) {
78             return null;
79         }
80         return getUserAuthorityService().getUser(null, username);//TODO context
81
}
82
83     public void setAuthenticatedUser(String JavaDoc username) {
84         UserDetails userDetails = getUserDetailsService().loadUserByUsername(username);
85         
86         if (userDetails == null) {
87             throw new JSException("User \"" + username + "\" not found");
88         }
89         
90         if (!userDetails.isAccountNonExpired()) {
91             throw new JSException("User \"" + username + "\" is expired");
92         }
93         
94         if (!userDetails.isAccountNonLocked()) {
95             throw new JSException("User \"" + username + "\" is locked");
96         }
97         
98         if (!userDetails.isCredentialsNonExpired()) {
99             throw new JSException("User \"" + username + "\" credentials are expired");
100         }
101         
102         if (!userDetails.isEnabled()) {
103             throw new JSException("User \"" + username + "\" is disabled");
104         }
105         
106         UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
107         SecurityContextHolder.getContext().setAuthentication(authenticationToken);
108     }
109
110     public void revertAuthenticatedUser() {
111         // TODO revert to previous principal
112
SecurityContextHolder.getContext().setAuthentication(null);
113     }
114
115 }
116
Popular Tags