Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ************************************************************************
  #
  #  DivConq
  #
  #  http://divconq.com/
  #
  #  Copyright:
  #    Copyright 2014 eTimeline, LLC. All rights reserved.
  #
 #  License:
 #    See the license.txt file in the project's top-level directory for details.
 #
 #  Authors:
 #    * Andy White
 #
 ************************************************************************ */
 package divconq.lang;
 
User Context works in conjunction with Task Context and Session to track information about Authenticated User or Guest User.

Author(s):
Andy
 
 public class UserContext {
 	static public UserContext allocate(OperationContextBuilder tcb) {
 		return new UserContext(tcb.values);
 	}

Returns:
create a new guest context
 
 	static public UserContext allocateGuest() {
 	}

Returns:
create a new root context
 
 	static public UserContext allocateRoot() {
 	}

Parameters:
m create a task context from a message (RPC calls to dcBus), keep in mind this is info gathering only, message must not be allowed to force an authenticated/elevated state inappropriately - from RPC clear "Elevated" field before calling this
Returns:
user context from the record
 
 	static public UserContext allocate(RecordStruct m) {
 		return new UserContext(m);
 	}
 	
 	static public UserContext allocateFromTask(RecordStruct m) {
 		return new UserContext(m.deepCopyExclude("OpId""SessionId""Origin""DebugLevel""Elevated""Pool"));
 	}
 	
 		if (ctx.context.isFieldEmpty("Credentials") && (v == null))
 			return null;
 		
 		// no need to do anything if the creds are the same
 		if (!ctx.context.isFieldEmpty("Credentials") && (v != null) && ctx.getCredentials().equals(v))		// TODO the equals is not right for deep records
 			return null;
 		
 		// when credentials change you are left with only guest access
 		return ctx.toBuilder().withCredentials(v);
 	}
 
 	// instance code
 	
 	protected RecordStruct context = null;

Returns:
the id of the user, if guest then "00000_000000000000002", if root then "00000_000000000000001"
 
 	public String getUserId() {
 		return this..getFieldAsString("UserId");
 	}

Returns:
the username of the user
 
 	public String getUserName() {
 		return this..getFieldAsString("UserName");
 	}

Returns:
the fullname of the user
 
 	public String getFullName() {
		return this..getFieldAsString("FullName");
	}

Returns:
the primary email of the user
	public String getEmail() {
		return this..getFieldAsString("Email");
	}

Returns:
the authentication token
	public String getAuthToken() {
		return this..getFieldAsString("AuthToken");
	}
		return this..getFieldAsRecord("Credentials");
	}

Verified tasks have been authenticated.

Returns:
true if task has been elevated
	public boolean isVerified() {
		return this..getFieldAsBooleanOrFalse("Verified");
	}

Returns:
chronology to use with this task (for output)
	public String getChronology() {
		return this..getFieldAsString("Chronology");
	}

Returns:
locale to use with this task for output
	public String getLocale() {
		return this..getFieldAsString("Locale");
	}

Domain indicates the domain targeted by a request.

Returns:
domain string
	public String getDomainId() {
		return this..getFieldAsString("DomainId");
	}
	public DomainInfo getDomain() {
	}

Parameters:
m create a task context from a message (RPC calls to dcBus), keep in mind this is info gathering only, message must not be allowed to force an authenticated/elevated state inappropriately - from RPC clear "Elevated" field before calling this
	protected UserContext(RecordStruct m) {
		this. = m;
	}
		return (RecordStructthis..deepCopy();
	}
	}


Parameters:
m store task context into a message - for context transfer over bus
	public void freeze(RecordStruct m) {
	}

Parameters:
m store task context into a message - for context transfer over bus
	public void freezeRpc(RecordStruct m) {
		// rpc doesn't get creds or token
		m.copyFields(this."Credentials""AuthToken");
	}
	public boolean looksLikeGuest() {
		if (!"00000_000000000000002".equals(this.getUserId()))
			return false;
		if (!this..isFieldEmpty("AuthToken") || !this..isFieldEmpty("Credentials"))
			return false;
		ListStruct creds = this..getFieldAsList("AuthTags");
		if ((creds == null) || (creds.getSize() != 1))
			return false;
		return ("Guest".equals(creds.getItemAsString(0)));
	}
	public boolean looksLikeRoot() {
		if (!"00000_000000000000001".equals(this.getUserId()))
			return false;
		if (!this..isFieldEmpty("AuthToken") || !this..isFieldEmpty("Credentials"))
			return false;
		ListStruct creds = this..getFieldAsList("AuthTags");
		if ((creds == null) || (creds.getSize() == 0))
			return false;
		for (int i = 0; i < creds.getSize(); i++) {
			if ("SysAdmin".equals(creds.getItemAsString(i)))
				return true;
		}
		return false;
	}

Parameters:
tags to search for with this user
Returns:
true if this user has one of the requested authorization tags (does not check authentication)
	public boolean isTagged(String... tags) {
		ListStruct creds = this..getFieldAsList("AuthTags");
		if (creds == null
			return false;
		for (int i = 0; i < creds.getSize(); i++) {
			String has = creds.getItemAsString(i);
			for (String wants : tags) {
				if (has.equals(wants))
					return true;
			}
		}
		return false;
	}
	public String tr(String tokenObject... params) {
		return LocaleUtil.tr(this.getLocale(), tokenparams);		
	}
	public String trp(String pluraltokenString singulartokenObject... params) {
		return LocaleUtil.trp(this.getLocale(), pluraltokensingulartokenparams);		
	}
	public String toString() {
		RecordStruct creds = usr.getFieldAsRecord("Credentials");
		if (creds != null) {
			if (creds.hasField("Password"))
				creds.setField("Password""*****");
		}
		return usr.toPrettyString(); 
	}
	public boolean equals(Object obj) {
		if (!(obj instanceof UserContext))
			return false;
		return this..equals(((UserContext)obj).);
	}
	public void freezeSafe(RecordStruct m) {
		RecordStruct creds = m.getFieldAsRecord("Credentials");
		if (creds != null) {
			if (creds.hasField("Password"))
				creds.setField("Password""*****");
		}
	}
New to GrepCode? Check out our FAQ X