Changing User Permissions
One very powerful feature of our API is the ability to manage users, devices, and layouts. It is a less obvious use for our API. It is less obvious because we focus some much on retrieving video through our API that some of the other features can be forgetten about.
In this example I will explain how to change user permissions in bulk. We had a customer that needs to set a specific set of permissions for several hundred users. Doing this manually is time consuming and error prone. The goal was to create a script that could quickly set all the permissions. A secondary bennefit to this, is that it can be run again in the future to ensure the permissions are still set correctly.
The customer had all the users in an Excel file. We needed to get the data in a data format that is easier to use. We had the customer export the data from Excel into a new file in the CSV format.
CSV stands for comma separated values. It is just taking each row of the excel, listing it as line of text with a column separating each column. Some of the fancier Excel features like pivot tables don't export very well to CSV, so make sure to check what data you are exporting.
Step 1: Understanding the Data
Starting with the CSV data we got from Excel, we have the email address in the first column, the user's first name in the second column, and their last name in the third column.
The minimum data need could be just the user's email address. This was fairly sparse, they could also add the permissions as additional columns for each user.
Step 2: Importing the Data
Now that we understand the data and have it as a CSV file, we want to import it. In this case we used the CSV module in Python. Almost every language tha has teh ability to read files can accomidate CSV files. That's the advantage of such a simple format.
For each for row in the CSV file, separate each item in order by breaking on the comma. Because of the rules around excaping characters can get weird, I suggest using an existing module.
Step 3: Looking up Users
We have the user's email address as the first item in the CSV file. Inside our code we need a way to match Eagle Eye users up with their email address. The easiest way to do this is to get all the users for an account and then find the user with the matching email address.
You can do this by pulling down a list of all users and then looking through the results for a matching email address. If this is a frequent operation you should consider caching this data for future lookups.
Step 4: Setting the Permissions
You can see how to update a user here. We are going to focus on putting together the JSON data as the request's payload.
The help file does a good job of explaining the different permissions and what they do. The user object has properties with the same names that start with 'is_'.
The image on the left shows an example of how to construce the payload. In this case we are giving the user all permissions except Administrator access.
Adjust it for your situation. Once you have the disired permissions set, you can go ahead make the request to udpate that user.
What else can we do with this?
This isn't the flashiest area of our API but it has the potential to save hours of time that would otherwise be spent manually updated users. This can also be extended to integrate with custom roles, LDAP, or Active Directory.
I hope you found this helpful. Please feel free to reach out to me directly at email@example.com