3add0e6a40b22d0745421d7f0d4db46a82babbfa
[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 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 public class DaylightWebUtil {
12
13     private static final String AUDIT = "audit";
14     private static final Logger logger = LoggerFactory.getLogger(AUDIT);
15
16     /**
17      * Returns the access privilege the user has on the specified container
18      *
19      * @param userName
20      *            The user name
21      * @param container
22      *            The container name. If null, the default container will be assumed
23      * @param bundle
24      *            The bundle originating the request
25      * @return The access privilege the user is granted on the container
26      */
27     public static Privilege getContainerPrivilege(String userName,
28             String container, Object bundle) {
29         // Derive the target resource
30         String resource = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
31
32         // Retrieve the Container Authorization service
33         IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper
34                 .getGlobalInstance(IContainerAuthorization.class, bundle);
35         if (auth != null) {
36             return auth.getResourcePrivilege(userName, resource);
37         }
38
39         /*
40          * Container Authorization service not available. We can only derive the
41          * access privilege to the default container based on user level
42          */
43         if (resource.equals(GlobalConstants.DEFAULT.toString())) {
44             IUserManager userManager = (IUserManager) ServiceHelper
45                     .getGlobalInstance(IUserManager.class, bundle);
46             if (userManager != null) {
47                 switch (userManager.getUserLevel(userName)) {
48                 case NETWORKADMIN:
49                     return Privilege.WRITE;
50                 case NETWORKOPERATOR:
51                     return Privilege.READ;
52                 default:
53                     return Privilege.NONE;
54                 }
55             }
56         }
57
58         return Privilege.NONE;
59     }
60
61     public static void auditlog(String moduleName, String user, String action, String resource,
62             String containerName) {
63         String auditMsg = "";
64         String mode = "WEB";
65         if (containerName != null) {
66             auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource + " in container "
67                     + containerName;
68         } else {
69             auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource;
70         }
71         logger.info(auditMsg);
72     }
73
74     public static void auditlog(String moduleName, String user, String action, String resource) {
75         auditlog(moduleName, user, action, resource, null);
76     }
77 }