View Javadoc

1   /*
2      Copyright 2008 Ramon Servadei
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  package fulmine.context;
17  
18  import fulmine.model.field.IField;
19  import fulmine.util.Utils;
20  
21  /**
22   * A permission profile that has permission only for the
23   * {@link IPermissionProfile#DEFAULT_APPLICATION default application} and
24   * {@link IPermissionProfile#DEFAULT_PERMISSION default permission code}. This
25   * means that, by default, this profile can view all {@link IField}s that are
26   * created with no application or permission details.
27   * <p>
28   * When application code provides a permission profile via
29   * {@link IFulmineContext#setPermissionProfile(IPermissionProfile)}, the
30   * application profile is merged with the default profile; this allows the
31   * application's context to still view fields that have no permission (they use
32   * the default application and permission).
33   * 
34   * @author Ramon Servadei
35   */
36  public class DefaultPermissionProfile implements IPermissionProfile
37  {
38      /** Holds application level permission profiles */
39      private IPermissionProfile applicationPermissions;
40  
41      public boolean contains(byte application, short permission)
42      {
43          if (application == IPermissionProfile.DEFAULT_APPLICATION
44              && permission == IPermissionProfile.DEFAULT_PERMISSION)
45          {
46              return true;
47          }
48          if (getApplicationPermissions() != null)
49          {
50              return getApplicationPermissions().contains(application, permission);
51          }
52          return false;
53      }
54  
55      public boolean matches(byte receivedApplication, short receivedPermission,
56          byte matchWithApplication, short matchWithPermission)
57      {
58          if (matchWithApplication == IPermissionProfile.DEFAULT_APPLICATION
59              && matchWithPermission == IPermissionProfile.DEFAULT_PERMISSION)
60          {
61              return true;
62          }
63          if (getApplicationPermissions() != null)
64          {
65              return getApplicationPermissions().matches(receivedApplication,
66                  receivedPermission, matchWithApplication, matchWithPermission);
67          }
68          return false;
69      }
70  
71      /**
72       * Get the application permissions
73       * 
74       * @see IFulmineContext#setPermissionProfile(IPermissionProfile)
75       * @return the permissions set by the application
76       */
77      public IPermissionProfile getApplicationPermissions()
78      {
79          return this.applicationPermissions;
80      }
81  
82      /**
83       * Set the application permissions
84       * 
85       * @see IFulmineContext#setPermissionProfile(IPermissionProfile)
86       * @param applicationPermissions
87       *            the permissions for the application
88       */
89      public void setApplicationPermissions(
90          IPermissionProfile applicationPermissions)
91      {
92          this.applicationPermissions = applicationPermissions;
93      }
94  
95      public byte getApplicationCode()
96      {
97          return this.applicationPermissions == null ? DEFAULT_APPLICATION
98              : this.applicationPermissions.getApplicationCode();
99      }
100 
101     public short getPermissionCode()
102     {
103         return this.applicationPermissions == null ? DEFAULT_PERMISSION
104             : this.applicationPermissions.getPermissionCode();
105     }
106 
107     @Override
108     public String toString()
109     {
110         return Utils.string(this, "application=" + getApplicationCode()
111             + ", permission=" + getPermissionCode());
112     }
113 }