EffectiveEntitlement entitlement = new EffectiveUserEntitlement(AnonymousUser.ANONYMOUS_ID, blog,
Entitlement.MODERATE, Entitlement.READ, Entitlement.WRITE);
*****************************************************************************************************************************************
/**
* Immutable expression of user entitlement within the system for a specific
* object. Of note is that this object exposes object type and content type - the
* object type is the object for which the entitlements have been captured. The
* content type is the child content to which the entitlement applies, possibly
* none in which case the entitlement applies to all content types.
*
* @Since 2.1
*/
public class EffectiveEntitlement implements ExternalizableLite {
private static final long serialVersionUID = -1938147856787179824L;
public static final int NONE = -1;
private static final int cacheSize =
CacheSizes.sizeOfInt() + //mask
CacheSizes.sizeOfInt() + //content type
CacheSizes.sizeOfLong() + //user id
CacheSizes.sizeOfLong() + //group id
CacheSizes.sizeOfLong() + //object id
CacheSizes.sizeOfInt() + //object type
CacheSizes.sizeOfInt() ; //hash code
private int entitlementMask = 0;
private long userId = 0;
private long groupId = 0;
private long objectId = 0;
private int objectType = 0;
private int contentType = 0;
private int hashCode = 0;
public EffectiveEntitlement(long userId, long groupId, long objectId, int objectType, int contentType, int entitlementMask) {
super();
this.entitlementMask = entitlementMask;
this.userId = userId;
this.groupId = groupId;
this.objectId = objectId;
this.objectType = objectType;
this.contentType = contentType;
setHash();
}
public EffectiveEntitlement(long userId, long groupId, long objectId, int objectType, int contentType, Entitlement entitlement) {
this(userId, groupId, objectId, objectType, contentType, entitlement.getID());
}
public EffectiveEntitlement(long userId, long groupId, long objectId, int objectType, int contentType, Entitlement... entitlements) {
super();
int mask = 0;
if(null != entitlements && entitlements.length != 0) {
for(Entitlement ent : entitlements) {
mask |= ent.getID();
}
}
this.entitlementMask = mask;
this.userId = userId;
this.groupId = groupId;
this.objectId = objectId;
this.objectType = objectType;
this.contentType = contentType;
setHash();
}
/**
* Create a new entitlement based on a deep copy of an existing entitlement representation.
* @param ee
*/
public EffectiveEntitlement(EffectiveEntitlement ee) {
this(ee.getUserID(), ee.getGroupID(), ee.getObjectID(), ee.getObjectType(), ee.getContentType(), ee.getEntitlementMask());
}
/**
* Creates a new effective entitlement for the given user id, copying the remainder
* of the entitlement from the specified data.
* @param ee - Entitlement to use as the source of the new entitlement data.
* @param userID - UserID to apply to the new entitlement.
*/
public EffectiveEntitlement(EffectiveEntitlement ee, long userID) {
this(userID, ee.getGroupID(), ee.getObjectID(), ee.getObjectType(), ee.getContentType(), ee.getEntitlementMask());
}
public long getUserID() {
return userId;
}
public long getGroupID() {
return groupId;
}
public long getObjectID() {
return objectId;
}
public int getObjectType() {
return objectType;
}
public int getContentType() {
return contentType;
}
public int getEntitlementMask() {
return entitlementMask;
}
public boolean isAuthorized(Entitlement toCheck) {
return (this.entitlementMask & toCheck.getID()) != 0;
}
public boolean isAuthorized(int mask) {
return (this.entitlementMask & mask) != 0;
}
public boolean isAuthorized(Entitlement... ents) {
int localMask = 0;
if(ents != null) {
for(Entitlement ent : ents) {
localMask |= ent.getID();
}
}
return (localMask & this.entitlementMask) != 0;
}
@Override
public String toString() {
if(userId <>
if(groupId == RegisteredUsersGroup.REGISTERED_USERS_GROUP_ID) {
if(contentType == EffectiveEntitlement.NONE) {
return String.format("[reg-users][oid:%s/%s]",
objectId, objectType);
}
else {
return String.format("[reg-users][oid:%s/%s][%s]",
objectId, objectType, contentType);
}
}
else if(groupId == -1) {
if(contentType == EffectiveEntitlement.NONE) {
return String.format("[anonymous][oid:%s/%s]",
objectId, objectType);
}
else {
return String.format("[anonymous][oid:%s/%s][%s]",
objectId, objectType, contentType);
}
}
else {
if(contentType == EffectiveEntitlement.NONE) {
return String.format("[group:%s][oid:%s/%s]",
groupId, objectId, objectType);
}
else {
return String.format("[group:%s][oid:%s/%s][%s]",
groupId, objectId, objectType, contentType);
}
}
}
else {
if(contentType == EffectiveEntitlement.NONE) {
return String.format("[user:%s][oid:%s/%s]",
userId, objectId, objectType);
}
else {
return String.format("[user:%s][oid:%s/%s][%s]",
userId, objectId, objectType, contentType);
}
}
}
@Override
public boolean equals(Object rhs0) {
if(null == rhs0) return false;
EffectiveEntitlement rhs = (EffectiveEntitlement)rhs0;
return (
this.entitlementMask == rhs.entitlementMask &&
this.userId == rhs.userId &&
this.groupId == rhs.groupId &&
this.objectId == rhs.objectId &&
this.objectType == rhs.objectType &&
this.contentType == rhs.contentType);
}
@Override
public int hashCode() {
return hashCode;
}
public void readExternal(DataInput in) throws IOException {
this.entitlementMask = in.readInt();
this.userId = in.readLong();
this.groupId = in.readLong();
this.objectId = in.readLong();
this.objectType = in.readInt();
this.contentType = in.readInt();
this.hashCode = in.readInt();
}
public void writeExternal(DataOutput out) throws IOException {
out.writeInt(this.entitlementMask);
out.writeLong(this.userId);
out.writeLong(this.groupId);
out.writeLong(this.objectId);
out.writeInt(this.objectType);
out.writeInt(this.contentType);
out.writeInt(this.hashCode);
}
public int getCacheSize() {
return cacheSize;
}
private void setHash() {
final long longValue = (this.entitlementMask * 2) + (this.userId * 3) + (this.objectId * 11) +
(this.objectType * 13) + (this.groupId * 17) + (this.contentType * 19);
hashCode = (int)(longValue^(longValue >>>32));
}
}
*************************************************************************************************************************************************
public enum Entitlement {
NONE(0x0),
READ(0x1),
WRITE(0x2),
MODERATE(0x4),
ADMINISTER(0x8);
private final int entitlementId;
Entitlement(int entitlementId) {
this.entitlementId = entitlementId;
}
public int getID() {
return this.entitlementId;
}
public int value() {
return this.entitlementId;
}
}
No comments:
Post a Comment