|
1 | | -# UnipyAccess API Connector |
2 | 1 |
|
3 | | -## Overview |
| 2 | +# unipyaccess |
4 | 3 |
|
5 | | -unipyAccess is a Python class designed to interface with the Unifi Access, allowing for the management of users in the system. This connector handles authentication, retrieval of users, creation, activation, deactivation, deletion, and updating of user groups. |
| 4 | +`unipyaccess` is a Python package designed to interface with the **Unifi Access** system. This package provides a simple and efficient way to manage users in Unifi Access, including authentication, retrieval, creation, activation, deactivation, deletion, and updating of user groups. |
6 | 5 |
|
7 | | -Disclaimer: This implementation does not use the new Unifi API. Instead, it utilizes authentication through a admin user account. |
| 6 | +> **Note**: This implementation uses Unifi API endpoints with admin user authentication. It does **not** utilize the latest, in my opinion half-baked Unifi API. |
| 7 | +
|
| 8 | +## Features |
| 9 | + |
| 10 | +- Authenticate with Unifi Access using admin credentials. |
| 11 | +- Retrieve, create, activate, deactivate, and delete user accounts. |
| 12 | +- Update user group assignments. |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +Install the package via `pip`: |
| 17 | + |
| 18 | +```bash |
| 19 | +pip install unipyaccess |
| 20 | +``` |
8 | 21 |
|
9 | 22 | ## Requirements |
10 | 23 |
|
11 | 24 | - Python 3.x |
12 | 25 | - `requests` library |
13 | 26 |
|
14 | | -To install the `requests` library, run: |
| 27 | +Install the requirements with: |
| 28 | + |
15 | 29 | ```bash |
16 | 30 | pip install requests |
17 | 31 | ``` |
18 | 32 |
|
19 | | -# Class: unipyAccess |
20 | | -## Initialization |
21 | | -The **unipyAccess** class requires the following parameters upon initialization: |
| 33 | +## Environment Setup |
22 | 34 |
|
23 | | -```python |
24 | | -unipy = unipyAccess(baseUrl, username, password, verify) |
| 35 | +Store your configuration details in a `.env` file: |
| 36 | + |
| 37 | +```bash |
| 38 | +UNIFI_CONTROLLER_ADDRESS=https://unifi-controller.local |
| 39 | +UNIFI_LOGIN=admin |
| 40 | +UNIFI_PASSWORD=password123 |
| 41 | +VERIFY_SSL=False |
25 | 42 | ``` |
26 | | -`baseUrl`: Internal URL of the Unifi controller (e.g., https://unifi-controller.local). |
27 | | -`username`: Username of the configuration user. |
28 | | -`password`: Password of the configuration user. |
29 | | -`verify`: Boolean (True/False) to indicate whether SSL verification should be performed on the requests. If None, verification defaults to True. |
30 | 43 |
|
31 | | -**Note**: Disabling SSL verification is not recommended in production environments as it can expose your application to security risks. |
| 44 | +## Usage |
32 | 45 |
|
33 | | -## Example |
34 | | -```python |
35 | | -from unipyAccess import unipyAccess |
36 | | -unipy = unipyAccess("https://unifi-controller.local", "unipy", "password123", verify=False) |
37 | | -``` |
38 | | -## Methods |
39 | | -### 1. getUnifiUsers() |
40 | | -Retrieves the list of users from the Unifi Access system. |
| 46 | +Import `unipyaccess` and use it in your Python script: |
41 | 47 |
|
42 | 48 | ```python |
43 | | -unipy.getUnifiUsers() |
44 | | -``` |
| 49 | +from unipyaccess import UnipyAccess |
| 50 | +from dotenv import load_dotenv |
| 51 | +import os |
45 | 52 |
|
46 | | -Example Response: |
47 | | -```json |
48 | | -[ |
| 53 | +load_dotenv() |
| 54 | + |
| 55 | +# Initialize the UnipyAccess API client |
| 56 | +unifiApi = UnipyAccess( |
| 57 | + base_url=os.getenv('UNIFI_CONTROLLER_ADDRESS'), |
| 58 | + username=os.getenv('UNIFI_LOGIN'), |
| 59 | + password=os.getenv('UNIFI_PASSWORD'), |
| 60 | + verify=os.getenv('VERIFY_SSL') |
| 61 | +) |
| 62 | + |
| 63 | +# Example: Create a new user |
| 64 | +users = [ |
49 | 65 | { |
50 | | - "id": "12345", |
51 | 66 | "first_name": "John", |
52 | 67 | "last_name": "Doe", |
53 | | - "employee_number": "54321" |
| 68 | + "PersonId": 124, |
| 69 | + "group_ids": ["bc1bf76d-5d2a-4a90-ae50-6aca02bccc63"] |
54 | 70 | } |
55 | 71 | ] |
| 72 | +unifiApi.create_unifi_users(users) |
56 | 73 | ``` |
57 | | -### 2. createUnifiUsers(users) |
58 | | -Creates new users in the Unifi Access system. |
59 | | - |
60 | | -`users`: List of dictionaries, where each dictionary represents a user with the following structure: |
61 | | -`first_name`: User's first name |
62 | | -`last_name`: User's last name |
63 | | -`PersonId`: Optional employee number (string) |
64 | | -`group_ids`: Optional list of group IDs to assign to the user. |
| 74 | + |
| 75 | +## Methods |
| 76 | + |
| 77 | +### 1. `get_unifi_users()` |
| 78 | +Fetches the list of users from Unifi Access. |
| 79 | + |
| 80 | +**Usage:** |
| 81 | + |
| 82 | +```python |
| 83 | +users = unifiApi.get_unifi_users() |
| 84 | +print(users) |
| 85 | +``` |
| 86 | + |
| 87 | +### 2. `create_unifi_users(users)` |
| 88 | +Creates new users. |
| 89 | + |
| 90 | +**Parameters:** |
| 91 | +- `users` (list): List of user dictionaries containing: |
| 92 | + - `first_name` (str): User's first name. |
| 93 | + - `last_name` (str): User's last name. |
| 94 | + - `PersonId` (str): Optional employee number. |
| 95 | + - `group_ids` (list): Optional list of group IDs. |
| 96 | + |
| 97 | +**Usage:** |
| 98 | + |
65 | 99 | ```python |
66 | 100 | users = [ |
67 | | - {"first_name": "Alice", "last_name": "Smith", "PersonId": 123, "group_ids": [1, 2]}, |
68 | | - {"first_name": "Bob", "last_name": "Jones", "PersonId": 456} |
| 101 | + {"first_name": "Jane", "last_name": "Doe", "PersonId": "789", "group_ids": ["group-123"]} |
69 | 102 | ] |
70 | | -unipy.createUnifiUsers(users) |
| 103 | +unifiApi.create_unifi_users(users) |
| 104 | +``` |
| 105 | + |
| 106 | +### 3. `deactivate_unifi_users(users)` |
| 107 | +Deactivates users. |
| 108 | + |
| 109 | +**Usage:** |
| 110 | + |
| 111 | +```python |
| 112 | +users = [{"id": "user-123"}] |
| 113 | +unifiApi.deactivate_unifi_users(users) |
71 | 114 | ``` |
72 | | -### 3. deactivateUnifiUsers(users) |
73 | | -Deactivates the given users. |
74 | 115 |
|
75 | | -`users`: List of dictionaries with the following structure: |
76 | | -`id`: User's unique ID |
| 116 | +### 4. `activate_unifi_users(users)` |
| 117 | +Activates users. |
| 118 | + |
| 119 | +**Usage:** |
| 120 | + |
77 | 121 | ```python |
78 | | -users = [{"id": "1bae4670-c853-4740-8bbc-62ff90aaad07"}] |
79 | | -unipy.deactivateUnifiUsers(users) |
| 122 | +users = [{"id": "user-123"}] |
| 123 | +unifiApi.activate_unifi_users(users) |
80 | 124 | ``` |
81 | | -### 4. activateUnifiUsers(users) |
82 | | -Activates the given users. |
83 | 125 |
|
84 | | -`users`: List of dictionaries with the following structure: |
85 | | -`id`: User's unique ID |
| 126 | +### 5. `delete_unifi_users(users)` |
| 127 | +Deletes users. |
| 128 | + |
| 129 | +**Usage:** |
86 | 130 |
|
87 | 131 | ```python |
88 | | -users = [{"id": "1bae4670-c853-4740-8bbc-62ff90aaad07"}] |
89 | | -unipy.activateUnifiUsers(users) |
| 132 | +users = [{"id": "user-123"}] |
| 133 | +unifiApi.delete_unifi_users(users) |
90 | 134 | ``` |
91 | | -### 5. deleteUnifiUsers(users) |
92 | | -Deletes users from the Unifi Access system. |
93 | 135 |
|
94 | | -`users`: List of dictionaries with the following structure: |
95 | | -`id`: User's unique ID |
| 136 | +### 6. `set_users_group(users)` |
| 137 | +Updates user group assignments. |
| 138 | + |
| 139 | +**Usage:** |
96 | 140 |
|
97 | 141 | ```python |
98 | | -users = [{"id": "1bae4670-c853-4740-8bbc-62ff90aaad07"}] |
99 | | -unipy.deleteUnifiUsers(users) |
| 142 | +users = [{"id": "user-123", "group": "group-456"}] |
| 143 | +unifiApi.set_users_group(users) |
100 | 144 | ``` |
101 | | -### 6. setUsersGroup(users) |
102 | | -Assigns or updates the group of the specified users. |
103 | 145 |
|
104 | | -`users`: List of dictionaries with the following structure: |
105 | | -`id`: User's unique ID |
106 | | -`group`: Group ID to assign to the user. |
| 146 | +## Example Code |
107 | 147 |
|
108 | 148 | ```python |
| 149 | +from unipyaccess import UnipyAccess |
| 150 | +from dotenv import load_dotenv |
| 151 | +import os |
| 152 | + |
| 153 | +load_dotenv() |
| 154 | + |
| 155 | +unifiApi = UnipyAccess( |
| 156 | + base_url=os.getenv('UNIFI_CONTROLLER_ADDRESS'), |
| 157 | + username=os.getenv('UNIFI_LOGIN'), |
| 158 | + password=os.getenv('UNIFI_PASSWORD'), |
| 159 | + verify=os.getenv('VERIFY_SSL') |
| 160 | +) |
| 161 | + |
| 162 | +# Retrieve users |
| 163 | +print(unifiApi.get_unifi_users()) |
| 164 | + |
| 165 | +# Create a user |
| 166 | +new_user = [{"first_name": "Alice", "last_name": "Smith", "PersonId": "125"}] |
| 167 | +unifiApi.create_unifi_users(new_user) |
| 168 | + |
| 169 | +# Activate a user |
| 170 | +unifiApi.activate_unifi_users([{"id": "user-123"}]) |
| 171 | + |
| 172 | +# Deactivate a user |
| 173 | +unifiApi.deactivate_unifi_users([{"id": "user-123"}]) |
| 174 | + |
| 175 | +# Delete a user |
| 176 | +unifiApi.delete_unifi_users([{"id": "user-123"}]) |
109 | 177 |
|
110 | | -users = [{"id": "12345", "group": 1}] |
111 | | -unipy.setUsersGroup(users) |
| 178 | +# Update user group |
| 179 | +unifiApi.set_users_group([{"id": "user-123", "group": "group-789"}]) |
112 | 180 | ``` |
113 | 181 |
|
114 | | -# License |
115 | | -This project is licensed under the MIT License. |
| 182 | +## License |
116 | 183 |
|
117 | | -This README file provides an overview, usage instructions, and example code to help users understand and use the `unipyAccess` class effectively. |
| 184 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details. |
0 commit comments