Java tutorial
/* * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * The Original Code is "stereotyped" (http://code.google.com/p/stereotyped). * * The Initial Developer of the Original Code is Richard Nichols. * Portions created by Richard Nichols are Copyright (C) 2010 * Richard Nichols. All Rights Reserved. * * Alternatively, the contents of this file may be used under the terms * of the GNU Public License (the "GPL"), in which case the * provisions of GPL License are applicable instead of those * above. If you wish to allow use of your version of this file only * under the terms of the GPL and not to allow others to use * your version of this file under the MPL, indicate your decision by * deleting the provisions above and replace them with the notice and * other provisions required by the GPL. If you do not delete * the provisions above, a recipient may use your version of this file * under either the MPL or the GPL License. */ package com.visural.stereotyped.ui; import com.google.common.collect.Sets; import com.visural.stereotyped.core.ServiceProvider; import com.visural.stereotyped.ui.model.StereotypeUIMetaData; import com.visural.stereotyped.ui.model.User; import com.visural.stereotyped.ui.model.UserGroup; import com.visural.stereotyped.ui.service.StereotypeService; import com.visural.wicket.security.IClient; import com.visural.wicket.security.IPrivilege; import java.util.UUID; /** * @version $Id$ * @author Visural */ public class Privilege { public static final IPrivilege ADMINISTRATOR = new IPrivilege() { public boolean isGrantedToClient(IClient arg0) { if (arg0 == null || !(arg0 instanceof User)) { return false; } else { return ((User) arg0).getGroups().contains(UserGroup.ADMINISTRATORS.getName()); } } }; public static IPrivilege currentUserIsOwner(final UUID id) { return new IPrivilege() { public boolean isGrantedToClient(IClient arg0) { StereotypeUIMetaData meta = ServiceProvider.getServiceInstance(StereotypeService.class) .readUIMeta(id); User u = (User) arg0; return meta.getOwner().equals(u.getId()); } }; } public static IPrivilege currentUserHasWriteAccess(final UUID id) { return new IPrivilege() { public boolean isGrantedToClient(IClient arg0) { if (id == null) { return true; } StereotypeUIMetaData meta = ServiceProvider.getServiceInstance(StereotypeService.class) .readUIMeta(id); User u = (User) arg0; return ADMINISTRATOR.isGrantedToClient(arg0) || meta.getOwner().equals(u.getId()) || meta.getEditUsers().contains(u.getId()) || !Sets.intersection(meta.getEditGroups(), u.getGroups()).isEmpty(); } }; } public static IPrivilege currentUserHasReadAccess(final UUID id) { return new IPrivilege() { public boolean isGrantedToClient(IClient arg0) { StereotypeUIMetaData meta = ServiceProvider.getServiceInstance(StereotypeService.class) .readUIMeta(id); User u = (User) arg0; return ADMINISTRATOR.isGrantedToClient(arg0) || meta.getOwner().equals(u.getId()) || meta.getViewUsers().contains(u.getId()) || meta.getEditUsers().contains(u.getId()) || !Sets.intersection(meta.getEditGroups(), u.getGroups()).isEmpty() || !Sets.intersection(meta.getViewGroups(), u.getGroups()).isEmpty(); } }; } public static IPrivilege currentUserIsOwnerOrAdmin(final UUID id) { return new IPrivilege() { public boolean isGrantedToClient(IClient arg0) { return ADMINISTRATOR.isGrantedToClient(arg0) || currentUserIsOwner(id).isGrantedToClient(arg0); } }; } }