Getting a reference to the current user

So I’ve got the following situation:

	'Users': collection ['Name'] {
		'Name': text
		'Type': stategroup (
			'Admin' { }
			'Unknown' { }
		)
	}

	'Questions': collection ['Question id']
	can-create: user
	can-delete: user .'Type'?'Admin' || user is ( >'User asked' )
	{
		can-update: user .'Type'?'Admin' || user is ( >'User asked' )
		'Question id': text
		'User asked': text -> ^ . 'Users'[] = ? // Get a reference to the current user
		'Title': text
		'Question text': text
		//...
	{

How can I get a reference to the current user so that when a question is created the ‘User asked’ field will point to the user that created it and the ‘can-delete’ and ‘can-update’ permissions could take place?

You currently have two options. You can provide an @default annotation. But, the user can then still change the value of User asked to point to another user:

'User asked': text -> ^ . 'Users'[] @default: user

To prevent that, you currently need a command:

'Submit question': command { 'ID': text } => switch user (
    | node as $'user' => update .'Questions' = create (
        'Question id' = @ .'ID'
        'User asked' = $'user'.'Name'
    )
    | none => ignore // question was not submitted by a signed-in user (but by a system)
)

We plan to support a solution that does not require a command as well.

1 Like