View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.tasksJsf;
18  
19  import javax.enterprise.context.Conversation;
20  import javax.enterprise.context.RequestScoped;
21  import javax.enterprise.inject.Produces;
22  import javax.faces.application.FacesMessage;
23  import javax.faces.context.FacesContext;
24  import javax.inject.Inject;
25  import javax.inject.Named;
26  
27  
28  /**
29   * Provides authentication operations with current user store: {@link Authentication}.
30   *
31   * @author Lukas Fryc
32   *
33   */
34  @Named
35  @RequestScoped
36  public class AuthController {
37  
38      @Inject
39      private Authentication authentication;
40  
41      @Inject
42      private UserDao userDao;
43  
44      @Inject
45      private FacesContext facesContext;
46  
47      @Inject
48      private Conversation conversation;
49  
50      /**
51       * <p>
52       * Provides current user to the context available for injection using:
53       * </p>
54       *
55       * <p>
56       * <code>@Inject @CurrentUser currentUser;</code>
57       * </p>
58       *
59       * <p>
60       * or from the Expression Language context using an expression <code>#{currentUser}</code>.
61       * </p>
62       *
63       * @return current authenticated user
64       */
65      @Produces
66      @Named
67      @CurrentUser
68      public User getCurrentUser() {
69          return authentication.getCurrentUser();
70      }
71  
72      /**
73       * <p>
74       * Authenticates current user with 'username' against user data store
75       * </p>
76       *
77       * <p>
78       * Starts the new conversation.
79       * </p>
80       *
81       * @param username the username of the user to authenticate
82       */
83      public void authenticate(String username) {
84          if (isLogged()) {
85              throw new IllegalStateException("User is logged and tries to authenticate again");
86          }
87  
88          User user = userDao.getForUsername(username);
89          if (user == null) {
90              user = createUser(username);
91          }
92          authentication.setCurrentUser(user);
93          conversation.begin();
94      }
95  
96      /**
97       * Logs current user out and ends the current conversation.
98       */
99      public void logout() {
100         authentication.setCurrentUser(null);
101         conversation.end();
102     }
103 
104     /**
105      * Returns true if user is logged in
106      *
107      * @return true if user is logged in; false otherwise
108      */
109     public boolean isLogged() {
110         return authentication.getCurrentUser() != null;
111     }
112 
113     private User createUser(String username) {
114         try {
115             User user = new User(username);
116             userDao.createUser(user);
117             facesContext.addMessage(null, new FacesMessage("User successfully created"));
118             return user;
119         } catch (Exception e) {
120             facesContext.addMessage(null, new FacesMessage("Failed to create user '" + username + "'", e.getMessage()));
121             return null;
122         }
123     }
124 }