View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.samples.sprhib.service;
20  
21  import org.apache.shiro.SecurityUtils;
22  import org.apache.shiro.crypto.hash.Sha256Hash;
23  import org.apache.shiro.samples.sprhib.dao.UserDAO;
24  import org.apache.shiro.samples.sprhib.model.User;
25  import org.springframework.beans.factory.annotation.Autowired;
26  import org.springframework.stereotype.Service;
27  import org.springframework.transaction.annotation.Transactional;
28  
29  import java.util.List;
30  
31  /**
32   * Default implementation of the {@link UserService} interface.  This service implements
33   * operations related to User data.
34   */
35  @Transactional
36  @Service("userService")
37  public class DefaultUserService implements UserService {
38  
39      private UserDAO userDAO;
40  
41      @Autowired
42      public void setUserDAO(UserDAO userDAO) {
43          this.userDAO = userDAO;
44      }
45  
46      public User getCurrentUser() {
47          final Long currentUserId = (Long) SecurityUtils.getSubject().getPrincipal();
48          if( currentUserId != null ) {
49              return getUser(currentUserId);
50          } else {
51              return null;
52          }
53      }
54  
55      public void createUser(String username, String email, String password) {
56          User user = new User();
57          user.setUsername(username);
58          user.setEmail(email);
59          user.setPassword( new Sha256Hash(password).toHex() );
60          userDAO.createUser( user );
61      }
62  
63      public List<User> getAllUsers() {
64          return userDAO.getAllUsers();
65      }
66  
67      public User getUser(Long userId) {
68          return userDAO.getUser(userId);
69      }
70  
71      public void deleteUser(Long userId) {
72          userDAO.deleteUser( userId );
73      }
74  
75      public void updateUser(User user) {
76          userDAO.updateUser( user );
77      }
78  
79  }