Maintain Additional User Attributes with JSON
PaperCut NG/MF maintains a wide variety of information for each user’s account. For example, email addresses, ID numbers (max 2), print and copy quota etc. This article will help advanced PaperCut NG/MF administrators who need to extend NG/MF with custom user attributes in a structured and future-proof manner.
Sometimes you need to record user information that PaperCut Software didn’t think of when we created the NG/MF database structure. For example, you might want to delete internal accounts three months after use, record external account IDs, or something else that is specific to your environment.
The good news is that every user account has a notes
property that you can use to store arbitrary text. However, as it’s a plain text field it can be hard to maintain anything more complex than a simple string, and even harder to extend use with new applications.
Fortunately JSON is a data format that stores complex data structures as text, and most modern programming languages provide libraries that make manipulating JSON data easy with native data structures such as arrays and associative arrays.
The benefit of encoding data as JSON in the Notes property is that we can add additional data for new applications as needed, without having to make changes to the existing applications that might use other JSON fields in our object. In addition, the data is stored as text and can be (carefully) updated via the NG/MF admin web interface.
Let’s look at a practical example from the world of public libraries. This library gives patrons access to a 3D printer lab using a 3rd party fab lab management system, and during the summer many tourists want to use the library on a short-term, temporary basis. To cater to that, in our Notes property will need to store a couple of values.
- A ‘fab lab’ account number - so if the user needs to use 3D printing, you can invoice the expensive 3D prints via PaperCut NG/MF.
- An account expiry date - only for internal users who are not in the library management system. Visitors create their own temporary accounts via self-registration.
To represent the values in JSON we can create a text string that looks like:
{"fablab-account":"1234567","expiry-date": "2020-01-29"}
using the correct JSON libraries to encode and decode these strings onto native data structures.
I have created a short example that uses Python to manage account expiry and updating the account expiry date. It works as follows:
After a visitor creates their temporary account there is no expiry date defined. So each night we can loop through all the internal user accounts and:
- Add an expiry date if one is missing.
- If the expiry date is in the past, delete the account.
The NG/MF web services API gives us an easy way to achieve this.
- Get a list of all the internal users (with the
getGroupMembers()
API call). - Retrieve the Notes property for each internal user (
getUserProperty()
API call). - Add a future expiry date if one is needed. The Python
datetime
library works well, but we need to use thedate.isoformat()
anddate.fromisoformat()
to convert to and from the date strings stored in the JSON objects. - Check the expiry date and delete the account if required (
deleteExistingUser
API call).
Notes
- When unmarshalling JSON from text you need to handle invalid JSON text and provide a reasonable fall back. For example, in Python:
try: userInfo = json.loads(notes) except: userInfo = {}
- As well as users, shared accounts and printers also have notes properties so you can use this idea in other places.
Categories: How-to Articles , Scripting and APIs
Keywords: JSON , notes , API , integration , extension , custom , customize , customise , user properties
Last updated June 13, 2024
Comments