Perspective - Log in / Log out event

Is there a way to get events on log in and log out ?

I want to load / save session custom value when user log and log out

I’d say the session.Startup and Shutdown events would fit the bill.

I want to save session value for each user, so I need to know the username, so session events not good for that.

Property change script on session.props.auth.user.id, or session.props.auth.securityLevels?

For sure, thanks

Have a trick to save the self.session.custom to a json field ?

What exactly are you trying to do?

I wanted to build a json object to save it in a database.

Finally I have builded a script to do that.

def SignSession(previousUser, currentUser, session, cell_path):
	db_available = system.tag.read("[System]Gateway/Database/server_amt/Available")
	if db_available:
		if not previousUser is None:
			if not previousUser.value is None:	
				my_data = session.custom
				my_session = dict()
				for i in range(len(list(my_data))):
					my_session[list(my_data)[i]] = my_data[list(my_data)[i]]
				my_session["lang"] = session.props.locale
				result = _Functions.SaveSession(my_session, previousUser.value, session.custom.cell_path)
		
		if not currentUser is None:
			if not currentUser.value is None:
				my_session = _Functions.LoadSession(session.custom.cell_path, currentUser.value)
				if len(my_session):
					my_session = system.util.jsonDecode(my_session[0][0])
					if "lang" in my_session:
						session.props.locale = my_session["lang"]
					for key in my_session:
						if not key == "lang":
							session.custom[key] = my_session[key]
					return
		
		_Functions.DefaultSession(session, cell_path)	
		
def SaveSession(session, username, cell_path):
	import json

	try:
		my_timestamp = str(fctTimestamp(setTimestamp(),False))
		system.db.runPrepUpdate("INSERT INTO tbl_session_data (cell_path, username, session_data, last_session) VALUES ('" + str(cell_path) + "', '" + str(username) + "', '" + json.dumps(session) + "', '" + my_timestamp + "') ON CONFLICT (cell_path, username) DO UPDATE SET session_data=excluded.session_data, last_session=excluded.last_session RETURNING id;", getKey=1)
		return True
	except:
		return False	
	
def LoadSession(cell_path, username):
	session_data = system.db.runPrepQuery("SELECT session_data FROM tbl_session_data WHERE cell_path='" + str(cell_path) + "' AND username='" + str(username) + "'")
	return session_data

You are not able to directly access the session.custom object; you would need to access session.custom.someProperty. I also don’t think list(my_data) will work because my_data would be an object, and as far as Jython is concerned, that’s equivalent to a dict already, so it looks like you’re obtaining a dict, then wrapping that dict in a list (so the list will only ever have one item), then iterating over that single-element list, and then attempting to assign the object as a dict key of the my_session object. Also, it looks like you’re passing in a cell_path variable but then you don’t use it because you only directly access a custom session property named cell_path, so watch out for that.

On my side all works well and did what I need.

Ah, it looks like what I said is only true while binding - disregard.

I have implemant a script on userName change value as you suggest me. All works well except a bizzare thing, I have to logout twice to be logged out correctly. At logout my script run correctly but user seems to still logged, when I logout an other times the user is logged out correctly.

The problems seems to be when I do the db transaction. When I skip the “SaveSession” I can logout by clicking 1 times.

Can you help me to fixed it?

My userName change event

_Session.SignSession(previousValue, currentValue, self, self.props.auth.authenticated)

My _Session module

def DefaultSession(session, cell_path):
	session.custom["cell_path"] = cell_path
	session.custom["unit_area"] = "square_feet"
	session.custom["unit_distance"] = "mm"
	session.custom["unit_force"] = "newton"
	session.custom["unit_pressure"] = "kPa"
	session.custom["unit_speed"] = "mm_per_second"
	session.custom["unit_temperature"] = "celsius"
	session.custom["unit_times"] = "millisecond"
	session.props.locale = session.custom.locale_default 
	return

def SignSession(previousUser, currentUser, session, auth):
	db_name = system.dataset.toPyDataSet(system.db.getConnectionInfo())[0][0]
	db_available = system.tag.read("[System]Gateway/Database/" + db_name + "/Available")
	if db_available:
		if auth:
			my_session = LoadSession(session.custom.cell_path, currentUser.value)
			if len(my_session):
				my_session = system.util.jsonDecode(my_session[0][0])
				if "lang" in my_session:
					session.props.locale = my_session["lang"]
				for key in my_session:
					if not key == session.props.locale:
						session.custom[key] = my_session[key]
		
		if not previousUser is None:
			if not previousUser.value is None:	
				my_data = session.custom
				my_session = dict()
				for i in range(len(list(my_data))):
					my_session[list(my_data)[i]] = my_data[list(my_data)[i]]
				my_session["lang"] = session.props.locale
				SaveSession(my_session, previousUser.value, session.custom.cell_path)
				
		if not auth:
			DefaultSession(session, session.custom.cell_path)
	return
		
def SaveSession(my_session, username, cell_path):
	import json

	try:
		my_timestamp = str(_Functions.fctTimestamp(_Functions.setTimestamp(),False))
		system.db.runPrepUpdate("INSERT INTO tbl_session_data (cell_path, username, session_data, last_session) VALUES ('" + str(cell_path) + "', '" + str(username) + "', '" + json.dumps(my_session) + "', '" + my_timestamp + "') ON CONFLICT (cell_path, username) DO UPDATE SET session_data=excluded.session_data, last_session=excluded.last_session;")
		return True
	except:
		return False	
	
def LoadSession(cell_path, username):
	session_data = system.db.runPrepQuery("SELECT session_data FROM tbl_session_data WHERE cell_path='" + str(cell_path) + "' AND username='" + str(username) + "'")
	return session_data

I am trying to attach a change script to the session.props.auth.user.id, but the script doesnt get saved. It disappears right after I press OK in the script edit window. Why does this happen?

I am able to add a change script to the property in the parent project, but not the one I am currently working in.
(8.1.27)