DEV Community

Peter + AI
Peter + AI

Posted on

Demystifying the $user Function in Uniface 10.4 πŸš€

Hey developers! πŸ‘‹ Ever worked with Uniface and needed to find out which user is currently active? There's a handy built-in function for that: $user. This post, created with some help from our AI friends, will break down how to use it in simple terms. Let's dive in!

What is the $user Function?

In a nutshell, the $user function returns the username of the current user. This is incredibly useful for tasks like auditing (tracking who did what), personalizing the user experience, or managing security within your application.

How to Use It βš™οΈ

The syntax is very straightforward. You can call the function in two main ways:

  • $user - Without any parameters to get the general system or web user.
  • $user(Path) - With a database path to get the user for that specific connection.

What Does It Return? πŸ€”

The value you get back from $user depends on the context where you use it:

1. For a Specific Database Path

When you use $user(MY_DB_PATH), it returns the username that was used to log into that specific database. Note that Uniface doesn't typically support spaces in these usernames; it will ignore anything after the first space.

2. In a Desktop Application (without a path)

  • On Windows: It looks for the user value in the [user] section of your usys.ini file.
  • On other Operating Systems (like Linux): It fetches the value from the USER environment variable.

If these are not set, you'll get an empty string.

3. In a Web Application (without a path)

In a web context, $user returns the authenticated username provided by the web server. Great news: in this scenario, usernames with spaces are supported! If the user isn't authenticated, or if there's a license issue, the function will return an empty string.

A Practical Example πŸ’‘

Imagine you want to log who creates a new record and who was the last person to update it. This is a classic auditing requirement. You can easily do this using the write trigger in a component. πŸ•΅οΈβ€β™€οΈ

; Write trigger for an entity

; $dbocc = 0 means the record is new (not yet in the database)
if ($dbocc = 0)
  CREATED_BY = $user
  CREATED_DATE = $date
else
  UPDATED_BY = $user
endif

write ; Proceeds with the actual database write
Enter fullscreen mode Exit fullscreen mode

In this snippet, if the record is being created for the first time, we populate the CREATED_BY field. If it's being updated, we populate the UPDATED_BY field instead.

Pro Tip πŸ§‘β€πŸ’»

You can pair $user with its sibling, $password, to seamlessly reconnect to a database. For instance, if you need to close and then reopen a database connection within your code, you can first store the results of $user and $password in variables. Then, you can use these variables in your

Top comments (0)