1   /*
2    * Copyright 2003 - 2013 The eFaps Team
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   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  
22  package org.efaps.bpm.identity;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.UUID;
27  
28  import org.efaps.admin.EFapsSystemConfiguration;
29  import org.efaps.admin.KernelSettings;
30  import org.efaps.admin.user.Person;
31  import org.efaps.admin.user.Role;
32  import org.efaps.util.EFapsException;
33  import org.kie.api.task.UserGroupCallback;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * TODO comment!
40   *
41   * @author The eFaps Team
42   * @version $Id$
43   */
44  public class UserGroupCallbackImpl
45      implements UserGroupCallback
46  {
47  
48      /**
49       * Logging instance used in this class.
50       */
51      private static final Logger LOG = LoggerFactory.getLogger(UserGroupCallbackImpl.class);
52  
53  
54      /**
55       * Default Constructor.
56       */
57      public UserGroupCallbackImpl()
58      {
59          try {
60              if (!EFapsSystemConfiguration.get().getAttributeValueAsBoolean(KernelSettings.REQUIRE_PERSON_UUID)) {
61                  UserGroupCallbackImpl.LOG.warn("BPM is activated but the UUID for Persons is not required. "
62                                  + "That might leed to serious problems in the future!!!!!!");
63              }
64          } catch (final EFapsException e) {
65              UserGroupCallbackImpl.LOG.error("Could not read SystemConfiguration.", e);
66          }
67      }
68  
69      /**
70       * Resolves existence of user id.
71       *
72       * By default, jBPM registers a special user with userId "Administrator" as the
73       * administrator of each task. You should therefore make sure that you always
74       * define at least a user "Administrator" when registering the list of valid
75       * users at the task service.
76       *
77       * @param _userId    the user id assigned to the task
78       * @return true if userId exists, false otherwise.
79       */
80      @Override
81      public boolean existsUser(final String _userId)
82      {
83          UserGroupCallbackImpl.LOG.debug("checking for existence of User: '{}'", _userId);
84          boolean ret = false;
85          if ("Administrator".equals(_userId)) {
86              ret = true;
87          } else {
88              try {
89                  final Person pers = org.efaps.admin.user.Person.get(UUID.fromString(_userId));
90                  ret = pers != null;
91              } catch (final EFapsException e) {
92                  UserGroupCallbackImpl.LOG.error("error while checkin for existence of User: '{}'", _userId);
93              }
94          }
95          UserGroupCallbackImpl.LOG.debug("result for existence check for User: '{}' is: {}", _userId, ret);
96          return ret;
97      }
98  
99      /**
100      * Resolves existence of group id.
101      *
102      * @param _groupId   the group id assigned to the task
103      * @return true if groupId exists, false otherwise.
104      */
105     @Override
106     public boolean existsGroup(final String _groupId)
107     {
108         UserGroupCallbackImpl.LOG.debug("checking for existence of Group: {}", _groupId);
109         boolean ret = false;
110         if ("Administrators".equals(_groupId)) {
111             ret = true;
112         } else {
113             try {
114                 final Role role = org.efaps.admin.user.Role.get(UUID.fromString(_groupId));
115                 ret = role != null;
116             } catch (final EFapsException e) {
117                 UserGroupCallbackImpl.LOG.error("error while checkin for existence of Group: '{}'", _groupId);
118             }
119         }
120         UserGroupCallbackImpl.LOG.debug("result for existence check for Group: '{}' is: {}", _groupId, ret);
121         return ret;
122     }
123 
124     /**
125      * Returns list of group ids for specified user id.
126      *
127      * @param _userId    the user id assigned to the task
128      * @param _groupIds  list of group ids assigned to the task
129      * @param _allExistingGroupIds   list of all currently known group ids
130      * @return List of group ids.
131      */
132     @Override
133     public List<String> getGroupsForUser(final String _userId,
134                                          final List<String> _groupIds,
135                                          final List<String> _allExistingGroupIds)
136     {
137         UserGroupCallbackImpl.LOG.debug(
138                         "getting Groups for User: '{}'. Assigned Groups: {}. Currently known Groups: {}.", _userId,
139                         _groupIds, _allExistingGroupIds);
140         final List<String> ret = new ArrayList<String>();
141         try {
142             final Person pers;
143             if ("Administrator".equals(_userId)) {
144                 pers = org.efaps.admin.user.Person.get(_userId);
145             } else {
146                 pers = org.efaps.admin.user.Person.get(UUID.fromString(_userId));
147             }
148             if (pers != null) {
149                 for (final Long roleId : pers.getRoles()) {
150                     final Role role = org.efaps.admin.user.Role.get(roleId);
151                     ret.add(role.getUUID().toString());
152                 }
153             }
154         } catch (final EFapsException e) {
155             UserGroupCallbackImpl.LOG.error("error while checkin for existence of User: '{}'", _userId);
156         }
157         UserGroupCallbackImpl.LOG.debug("found Groups for User: '{}': {}", _userId, ret);
158         return ret;
159     }
160 
161 }