KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > ModelPermission


1 /*
2  * $Id: ModelPermission.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service;
26
27 import org.ofbiz.entity.GenericValue;
28 import org.ofbiz.entity.GenericDelegator;
29 import org.ofbiz.entity.GenericEntityException;
30 import org.ofbiz.entity.util.EntityUtil;
31 import org.ofbiz.security.Security;
32 import org.ofbiz.base.util.UtilMisc;
33 import org.ofbiz.base.util.Debug;
34
35 import java.util.List JavaDoc;
36 import java.io.Serializable JavaDoc;
37
38 /**
39  * Service Permission Model Class
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 5462 $
43  * @since 3.0
44  */

45 public class ModelPermission implements Serializable JavaDoc {
46
47     public static final String JavaDoc module = ModelPermission.class.getName();
48
49     public static final int PERMISSION = 1;
50     public static final int ENTITY_PERMISSION = 2;
51     public static final int ROLE_MEMBER = 3;
52
53     public ModelService serviceModel = null;
54     public int permissionType = 0;
55     public String JavaDoc nameOrRole = null;
56     public String JavaDoc action = null;
57
58     public boolean evalPermission(Security security, GenericValue userLogin) {
59         if (userLogin == null) {
60             Debug.logInfo("Secure service requested with no userLogin object", module);
61             return false;
62         }
63         switch (permissionType) {
64             case 1:
65                 return evalSimplePermission(security, userLogin);
66             case 2:
67                 return evalEntityPermission(security, userLogin);
68             case 3:
69                 return evalRoleMember(userLogin);
70             default:
71                 Debug.logWarning("Invalid permission type [" + permissionType + "] for permission named : " + nameOrRole + " on service : " + serviceModel.name, module);
72                 return false;
73         }
74     }
75
76     private boolean evalSimplePermission(Security security, GenericValue userLogin) {
77         if (nameOrRole == null) {
78             Debug.logWarning("Null permission name passed for evaluation", module);
79             return false;
80         }
81         return security.hasPermission(nameOrRole, userLogin);
82     }
83
84     private boolean evalEntityPermission(Security security, GenericValue userLogin) {
85         if (nameOrRole == null) {
86             Debug.logWarning("Null permission name passed for evaluation", module);
87             return false;
88         }
89         if (action == null) {
90             Debug.logWarning("Null action passed for evaluation", module);
91         }
92         return security.hasEntityPermission(nameOrRole, action, userLogin);
93     }
94
95     private boolean evalRoleMember(GenericValue userLogin) {
96         if (nameOrRole == null) {
97             Debug.logWarning("Null role type name passed for evaluation", module);
98             return false;
99         }
100         GenericDelegator delegator = userLogin.getDelegator();
101         List JavaDoc partyRoles = null;
102         try {
103             partyRoles = delegator.findByAnd("PartyRole", UtilMisc.toMap("roleTypeId", nameOrRole, "partyId", userLogin.get("partyId")));
104         } catch (GenericEntityException e) {
105             Debug.logError(e, "Unable to lookup PartyRole records", module);
106         }
107
108         if (partyRoles != null && partyRoles.size() > 0) {
109             partyRoles = EntityUtil.filterByDate(partyRoles);
110             if (partyRoles != null && partyRoles.size() > 0) {
111                 return true;
112             }
113         }
114         return false;
115     }
116 }
117
Popular Tags