}
});
}
public static void setupSecurity() {
Scriptable global = GlobalData.getGlobalScope();
ScriptableObject security = new NativeObject();
global.put("security", global, security);
global.put("createUser", global, createUserHandler);
delegateToGlobalHandler(security,"createUser");
global.put("grantAccess", global, grantAccessHandler);
delegateToGlobalHandler(security,"grantAccess");
global.put("authenticate", global, authenticateHandler);
delegateToGlobalHandler(security,"authenticate");
global.put("getAccessLevel", global, getAccessLevelHandler);
delegateToGlobalHandler(security,"getAccessLevel");
global.put("hasAccessLevel", global, hasAccessLevelHandler);
delegateToGlobalHandler(security,"hasAccessLevel");
global.put("hasPermission", global, hasPermissionHandler);
delegateToGlobalHandler(security,"hasPermission");
security.put("getCapabilities", security, new PersevereNativeFunction() {
@Override
public Object call(final Context cx, final Scriptable scope,
final Scriptable thisObj, Object[] args) {
Object user;
if(args.length == 0 || args[0] == Undefined.instance){
user = UserSecurity.currentUser();
}else if (args[0] instanceof String){
user = CapabilityUser.getUserByUsername((String)args[0]);
}else
user = args[0];
Capability[] capabilities = getCapabilitiesForUser(user);
List capArray = Persevere.newArray();
if(capabilities != null)
capArray.addAll(Arrays.asList(capabilities));
return capArray;
}
public String toString() {
return "function(target,resource,accessLevel){/*native code*/}";
}
});
security.put("_refresh", security, new PersevereNativeFunction() {
@Override
public Object call(final Context cx, final Scriptable scope,
final Scriptable thisObj, Object[] args) {
Capability capability = ((Capability)args[0]);
capability.allGranted = null;
capability.computedPermissions.clear();
Transaction currentTransaction = Transaction.suspendTransaction();
List<Object> oldMembers = new ArrayList(capability.getAllMembers());
if(currentTransaction!=null) currentTransaction.enterTransaction();
List<Object> newMembers = args.length == 1 ?
new ArrayList(capability.getAllMembers()) :
new ArrayList();
oldMembers.removeAll(newMembers);
for (Object member : oldMembers){
capability.removeFromUserCapabilities(member);
}
for (Object member : newMembers){
capability.addToUserCapabilities(member);
}
return true;
}
public String toString() {
return "function(content, target, property){/*native code*/}";
}
});
security.put("changePassword", security, new PersevereNativeFunction() {
@Override
public Object call(final Context cx, final Scriptable scope,
final Scriptable thisObj, Object[] args) {
final Object user = UserSecurity.currentUser();
if(user == null){
throw new SecurityException("Can not change the user's password, no user is logged in");
}
if(!(user instanceof CapabilityUser)){
throw new SecurityException("Can not change the user's password, the current user is not Persevere defined user");
}
((CapabilityUser)user).setPassword(args[0].toString());
return true;
}
public String toString() {
return "function(newPassword){/*native code*/}";
}
});
security.setGetterOrSetter("currentUser", 0, new PersevereNativeFunction() {
@Override
public Object call(Context cx, Scriptable scope,
Scriptable thisObj, Object[] args) {
return UserSecurity.currentUser();
}