Adding container authorization code in web bundles
[controller.git] / opendaylight / web / root / src / main / java / org / opendaylight / controller / web / DaylightWebUtil.java
1 package org.opendaylight.controller.web;
2
3 import org.opendaylight.controller.containermanager.IContainerAuthorization;
4 import org.opendaylight.controller.sal.authorization.Privilege;
5 import org.opendaylight.controller.sal.utils.GlobalConstants;
6 import org.opendaylight.controller.sal.utils.ServiceHelper;
7 import org.opendaylight.controller.usermanager.IUserManager;
8
9 public class DaylightWebUtil {
10
11     /**
12      * Returns the access privilege the user has on the specified container
13      *
14      * @param userName
15      *            The user name
16      * @param container
17      *            The container name. If null, the default container will be assumed
18      * @param bundle
19      *            The bundle originating the request
20      * @return The access privilege the user is granted on the container
21      */
22     public static Privilege getContainerPrivilege(String userName,
23             String container, Object bundle) {
24         // Derive the target resource
25         String resource = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
26
27         // Retrieve the Container Authorization service
28         IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper
29                 .getGlobalInstance(IContainerAuthorization.class, bundle);
30         if (auth != null) {
31             return auth.getResourcePrivilege(userName, resource);
32         }
33
34         /*
35          * Container Authorization service not available. We can only derive the
36          * access privilege to the default container based on user level
37          */
38         if (resource.equals(GlobalConstants.DEFAULT.toString())) {
39             IUserManager userManager = (IUserManager) ServiceHelper
40                     .getGlobalInstance(IUserManager.class, bundle);
41             if (userManager != null) {
42                 switch (userManager.getUserLevel(userName)) {
43                 case NETWORKADMIN:
44                     return Privilege.WRITE;
45                 case NETWORKOPERATOR:
46                     return Privilege.READ;
47                 default:
48                     return Privilege.NONE;
49                 }
50             }
51         }
52
53         return Privilege.NONE;
54     }
55 }