1 25 26 package org.snipsnap.user; 27 28 import org.snipsnap.snip.Ownable; 29 import org.snipsnap.snip.Snip; 30 import org.snipsnap.container.Components; 31 32 38 39 public class Security { 40 46 public static Roles getRoles(User user) { 47 Roles userRoles = new Roles(user.getRoles()); 48 AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class); 49 50 if (service.isAuthenticated(user)) { 51 userRoles.add(Roles.AUTHENTICATED); 52 } 53 return userRoles; 54 } 55 56 63 public static Roles getRoles(User user, Snip object) { 64 Roles roles = getRoles(user); 65 if (object instanceof Ownable) { 66 Ownable o = object; 67 if (o.isOwner(user)) { 68 roles.add(Roles.OWNER); 69 } 70 } 71 return roles; 72 } 73 74 84 public static boolean existsPermission(String permission, Snip object, Roles roles) { 85 Permissions permissions = object.getPermissions(); 86 return permissions.exists(permission, roles); 87 } 88 89 public static boolean hasRoles(User user, Roles roles) { 90 Roles userRoles = getRoles(user); 91 return userRoles.containsAny(roles); 92 } 93 94 public static boolean hasRoles(User user, Snip object, Roles roles) { 95 Roles userRoles = getRoles(user, object); 96 return userRoles.containsAny(roles); 97 } 98 99 108 public static boolean checkPermission(String permission, User user, Snip object) { 109 Permissions permissions = object.getPermissions(); 110 if (null == permissions || permissions.empty()) { 111 return true; 112 } else { 113 return permissions.check(permission, getRoles(user, object)); 114 } 115 } 116 } 117 | Popular Tags |