001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.client.tide.javafx.spring;
022
023 import java.util.HashMap;
024 import java.util.Map;
025
026 import javax.inject.Named;
027
028 import org.granite.client.tide.javafx.BaseIdentity;
029 import org.granite.client.tide.javafx.ObservablePermission;
030 import org.granite.client.tide.javafx.ObservableRole;
031 import org.granite.client.tide.server.ServerSession;
032 import org.granite.client.util.WeakIdentityHashMap;
033 import org.granite.messaging.amf.RemoteClass;
034
035 /**
036 * @author William DRAI
037 */
038 @RemoteClass("org.granite.tide.spring.security.Identity")
039 @Named
040 public class Identity extends BaseIdentity {
041
042 protected Identity() {
043 // CDI proxying...
044 }
045
046 public Identity(final ServerSession serverSession) {
047 super(serverSession);
048 }
049
050
051 private Map<String, ObservableRole> ifAllGrantedCache = new HashMap<String, ObservableRole>();
052 private Map<String, ObservableRole> ifAnyGrantedCache = new HashMap<String, ObservableRole>();
053 private Map<String, ObservableRole> ifNotGrantedCache = new HashMap<String, ObservableRole>();
054
055
056 public ObservableRole hasRole(String roleName) {
057 return ifAllGranted(roleName);
058 }
059
060 public ObservableRole ifAllGranted(String roleName) {
061 ObservableRole role = ifAllGrantedCache.get(roleName);
062 if (role == null) {
063 role = new ObservableRole(Identity.this, getContext(), getServerSession(), "ifAllGranted", roleName);
064 ifAllGrantedCache.put(roleName, role);
065 }
066 return role;
067 }
068
069 public ObservableRole ifAnyGranted(String roleName) {
070 ObservableRole role = ifAnyGrantedCache.get(roleName);
071 if (role == null) {
072 role = new ObservableRole(Identity.this, getContext(), getServerSession(), "ifAnyGranted", roleName);
073 ifAnyGrantedCache.put(roleName, role);
074 }
075 return role;
076 }
077
078 public ObservableRole ifNotGranted(String roleName) {
079 ObservableRole role = ifNotGrantedCache.get(roleName);
080 if (role == null) {
081 role = new ObservableRole(Identity.this, getContext(), getServerSession(), "ifNotGranted", roleName);
082 ifNotGrantedCache.put(roleName, role);
083 }
084 return role;
085 }
086
087 private Map<Object, Map<String, ObservablePermission>> permissionsCache = new WeakIdentityHashMap<Object, Map<String, ObservablePermission>>();
088
089 public ObservablePermission hasPermission(Object entity, String action) {
090 Map<String, ObservablePermission> entityPermissions = permissionsCache.get(entity);
091 if (entityPermissions == null) {
092 entityPermissions = new HashMap<String, ObservablePermission>();
093 permissionsCache.put(entity, entityPermissions);
094 }
095 ObservablePermission permission = entityPermissions.get(action);
096 if (permission == null) {
097 permission = new ObservablePermission(this, getContext(), getServerSession(), "hasPermission", entity, action);
098 entityPermissions.put(action, permission);
099 }
100 return permission;
101 }
102
103 @Override
104 protected void initSecurityCache() {
105 for (ObservableRole role : ifAllGrantedCache.values())
106 role.clear();
107 for (ObservableRole role : ifAnyGrantedCache.values())
108 role.clear();
109 for (ObservableRole role : ifNotGrantedCache.values())
110 role.clear();
111
112 for (Map<String, ObservablePermission> entityPermissions : permissionsCache.values()) {
113 for (ObservablePermission permission : entityPermissions.values())
114 permission.clear();
115 }
116 }
117
118 /**
119 * Clear the security cache
120 */
121 @Override
122 public void clearSecurityCache() {
123 for (ObservableRole role : ifAllGrantedCache.values())
124 role.clear();
125 ifAllGrantedCache.clear();
126 for (ObservableRole role : ifAnyGrantedCache.values())
127 role.clear();
128 ifAnyGrantedCache.clear();
129 for (ObservableRole role : ifNotGrantedCache.values())
130 role.clear();
131 ifNotGrantedCache.clear();
132
133 for (Map<String, ObservablePermission> entityPermissions : permissionsCache.values()) {
134 for (ObservablePermission permission : entityPermissions.values())
135 permission.clear();
136 }
137 permissionsCache.clear();
138 }
139 }
140