View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.list.common.provider;
23  
24  import org.opencastproject.list.api.ResourceListProvider;
25  import org.opencastproject.list.api.ResourceListQuery;
26  import org.opencastproject.security.api.Role;
27  import org.opencastproject.security.api.User;
28  import org.opencastproject.security.api.UserDirectoryService;
29  
30  import org.apache.commons.lang3.StringUtils;
31  import org.osgi.framework.BundleContext;
32  import org.osgi.service.component.annotations.Activate;
33  import org.osgi.service.component.annotations.Component;
34  import org.osgi.service.component.annotations.Reference;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  import java.util.HashMap;
39  import java.util.Iterator;
40  import java.util.Map;
41  
42  @Component(
43      service = ResourceListProvider.class,
44      property = {
45          "service.description=Users list provider",
46          "opencast.service.type=org.opencastproject.list.provider.UsersListProvider"
47      }
48  )
49  public class UsersListProvider implements ResourceListProvider {
50  
51    private static final String PROVIDER_PREFIX = "USERS";
52  
53    public static final String NAME = PROVIDER_PREFIX + ".NAME"; // Username : Name
54    public static final String NAME_AND_EMAIL = PROVIDER_PREFIX + ".NAME.AND.EMAIL"; // Username: Name <Email>
55    public static final String NAME_AND_USERNAME = PROVIDER_PREFIX + ".NAME.AND.USERNAME"; // Username: Name (Username)
56    public static final String USERNAME = PROVIDER_PREFIX + ".USERNAME"; // UserName: UserName
57    public static final String EMAIL = PROVIDER_PREFIX + ".EMAIL"; // UserName: Email
58  
59    public static final String NAME_ONLY = PROVIDER_PREFIX + ".NAME.ONLY"; // Name : Name
60    public static final String EMAIL_ONLY = PROVIDER_PREFIX + ".EMAIL.ONLY"; // Email: Email
61    public static final String ROLE_ONLY = PROVIDER_PREFIX + ".ROLE.ONLY"; // Role: Role
62    // UserDirectory: UserDirectory
63    public static final String USERDIRECTORY_ONLY = PROVIDER_PREFIX + ".USERDIRECTORY.ONLY";
64  
65    protected static final String[] NAMES = { NAME, NAME_AND_EMAIL, NAME_AND_USERNAME, USERNAME, EMAIL, NAME_ONLY,
66        EMAIL_ONLY, ROLE_ONLY, USERDIRECTORY_ONLY };
67  
68    private static final Logger logger = LoggerFactory.getLogger(UsersListProvider.class);
69  
70    private UserDirectoryService userDirectoryService;
71  
72    @Activate
73    protected void activate(BundleContext bundleContext) {
74      logger.info("Users list provider activated!");
75    }
76  
77    /** OSGi callback for users services. */
78    @Reference
79    public void setUserDirectoryService(UserDirectoryService userDirectoryService) {
80      this.userDirectoryService = userDirectoryService;
81    }
82  
83    @Override
84    public String[] getListNames() {
85      return NAMES;
86    }
87  
88    @Override
89    public Map<String, String> getList(String listName, ResourceListQuery query) {
90      Map<String, String> usersList = new HashMap<>();
91      int offset = 0;
92      int limit = 0;
93  
94      if (query != null) {
95        if (query.getLimit().isPresent()) {
96          limit = query.getLimit().get();
97        }
98  
99        if (query.getOffset().isPresent()) {
100         offset = query.getOffset().get();
101       }
102     }
103 
104     Iterator<User> users = userDirectoryService.findUsers("%", offset, limit);
105 
106     while (users.hasNext()) {
107       User u = users.next();
108       if (EMAIL.equals(listName) && StringUtils.isNotBlank(u.getEmail())) {
109         usersList.put(u.getUsername(), u.getEmail());
110       } else if (USERNAME.equals(listName) && StringUtils.isNotBlank(u.getUsername())) {
111         usersList.put(u.getUsername(), u.getUsername());
112       } else if (NAME.equals(listName) && StringUtils.isNotBlank(u.getName())) {
113         usersList.put(u.getUsername(), u.getName());
114       } else if (NAME.equals(listName)
115               || NAME_AND_EMAIL.equals(listName)
116               || NAME_AND_USERNAME.equals(listName)) {
117         usersList.put(u.getUsername(), createDisplayName(u, listName));
118       } else if (NAME_ONLY.equals(listName) && StringUtils.isNotBlank(u.getName())) {
119         usersList.put(u.getName(), u.getName());
120       } else if (EMAIL_ONLY.equals(listName) && StringUtils.isNotBlank(u.getEmail())) {
121         usersList.put(u.getEmail(), u.getEmail());
122       } else if (USERDIRECTORY_ONLY.equals(listName) && StringUtils.isNotBlank(u.getProvider())) {
123         usersList.put(u.getProvider(), u.getProvider());
124       } else if (ROLE_ONLY.equals(listName) && u.getRoles().size() > 0) {
125         for (Role role : u.getRoles()) {
126           usersList.put(role.getName(), role.getName());
127         }
128       }
129     }
130     return usersList;
131   }
132 
133   /**
134    * Returns the name of the user as to be displayed in user interfaces.
135    *
136    * @param user
137    *          the user a displayable name should be generated for
138    * @param listName
139    *          the list name displayable names should be generated for
140    * @return name
141    *          a non-null string containing the name of the user to be displayed in user interfaces
142    */
143   private String createDisplayName(User user, String listName) {
144     assert ((user != null) && (user.getUsername() != null));
145     String name = StringUtils.isNotBlank(user.getName()) ? user.getName() : user.getUsername();
146     if (StringUtils.isNotBlank(user.getEmail()) && NAME_AND_EMAIL.equals(listName)) {
147       name = name + " <" + user.getEmail() + ">";
148     } else if (NAME_AND_USERNAME.equals(listName)) {
149       name = name + " (" + user.getUsername() + ")";
150     }
151     assert (name != null);
152     return name;
153   }
154 
155   @Override
156   public boolean isTranslatable(String listName) {
157     return false;
158   }
159 
160   @Override
161   public String getDefault() {
162     return null;
163   }
164 }