Users#
A heedy instance can have multiple users, all of which have different apps, and their own objects. This page describes accessing users and their properties from the Heedy Python client.
Listing & Accessing#
There are two modes of access to users. The first, which allows listing and accessing arbitrary users in the Heedy instance is only available to plugins. A list of all users in the instance can be retrieved with the plugin.users()
function from Users
:
p = heedy.Plugin(session="sync")
p.users() # Returns a list of all users
p = heedy.Plugin()
await p.users() # Returns a list of all users
To get a specific user by username, it is sufficient to access the users
object as a dict:
p = heedy.Plugin(session="sync")
p.users["myuser"] # Returns user w/ username "myuser"
p = heedy.Plugin()
p.users["myuser"] # Returns user w/ username "myuser"
On the other hand, when accessing from an app, the user that owns of the app can be accessed directly (if the app has the owner:read
scope!):
app = heedy.App("your_access_token","http://localhost:1324")
app.owner # Returns the User object of the app's owner
app = heedy.App("your_access_token","http://localhost:1324",session="async")
await app.owner # Returns the User object of the app's owner
Each App
and Object
element has a corresponding owner
property/attribute,
which can be read by both plugins and apps.
Creating & Deleting#
A Heedy plugin can create/delete users, but an app cannot. Users can be added using the create()
method:
usr = p.users.create("myusername","mypassword")
usr = await p.users.create("myusername","mypassword")
Likewise, once a User
object is retrieved, the user can be deleted by calling its delete()
method:
usr.delete()
await usr.delete()
Reading & Updating Properties#
Properties available on a user can be accessed directly as properties or attributes in the User
object:
# Does not use a server query, since username is the user object's ID
print(usr.username)
# Reads the user's display name from the server
print(usr.name)
# Uses the previously read cached data, avoiding a server query
print(usr["name"])
# Does not use a server query, since username is the user object's ID
print(usr.username)
# Reads the user's display name from the server
print(await usr.name)
# Uses the previously read cached data, avoiding a server query
print(usr["name"])
To update the corresponding properties, the update function can be used, or if in a sync session, the properties can also be directly set:
usr.name = "Alan Turing" # Sets the user's display name
# The update function allows setting multiple properties in a single query
usr.update(description="I like computers")
# The update function allows setting properties for the user:
await usr.update(name="Alan Turing", description="I like computers")
Updating User Password#
Since heedy plugins have full access to the Heedy database, they can also change a user’s password. Passwords in Heedy are hashed, so it is not possible to read an existing password, but it can be updated like a normal property:
usr.password= "newpassword"
await usr.update(password="newpassword")