Skip to content

Commit cb8807a

Browse files
committed
add user management example
1 parent 9dbc582 commit cb8807a

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed

examples/01_users.ipynb

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"name": "python",
5+
"display_name": "Python (Pyodide)",
6+
"language": "python"
7+
},
8+
"language_info": {
9+
"codemirror_mode": {
10+
"name": "python",
11+
"version": 3
12+
},
13+
"file_extension": ".py",
14+
"mimetype": "text/x-python",
15+
"name": "python",
16+
"nbconvert_exporter": "python",
17+
"pygments_lexer": "ipython3",
18+
"version": "3.8"
19+
},
20+
"colab": {
21+
"provenance": [],
22+
"collapsed_sections": [
23+
"x8IVfy9K5Z4l",
24+
"7VMIPQo2yTmQ"
25+
],
26+
"authorship_tag": "ABX9TyPXXcFNdfLOsA7CkWfkKXfJ"
27+
}
28+
},
29+
"nbformat_minor": 4,
30+
"nbformat": 4,
31+
"cells": [
32+
{
33+
"cell_type": "markdown",
34+
"source": "# Mergin Maps Users Management",
35+
"metadata": {
36+
"id": "ngTJ3YbHlb8s"
37+
}
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"source": "Mergin Maps API allows you to fully manage your workspace users, it's roles and project permissions.",
42+
"metadata": {
43+
"id": "q0eLjSMzlwdx"
44+
}
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"source": "First let's install mergin maps client",
49+
"metadata": {
50+
"id": "IKmFEjG-mmL6"
51+
}
52+
},
53+
{
54+
"cell_type": "code",
55+
"source": "!pip install mergin-client",
56+
"metadata": {
57+
"id": "I_vpP6NnmqV7"
58+
},
59+
"outputs": [],
60+
"execution_count": null
61+
},
62+
{
63+
"cell_type": "markdown",
64+
"source": "Login to Mergin Maps using your workspace user with `Owner` permission.",
65+
"metadata": {
66+
"id": "u05lxbRQm2VF"
67+
}
68+
},
69+
{
70+
"cell_type": "code",
71+
"source": "# Use here your login username and password\nLOGIN=\"\"\nPASSW=\"\"\n\nimport mergin\n\nclient = mergin.MerginClient(login=LOGIN, password=PASSW)",
72+
"metadata": {
73+
"colab": {
74+
"base_uri": "https://localhost:8080/"
75+
},
76+
"id": "dWQorVqZnRNl",
77+
"executionInfo": {
78+
"status": "ok",
79+
"timestamp": 1748364457967,
80+
"user_tz": -60,
81+
"elapsed": 1822,
82+
"user": {
83+
"displayName": "Fernando Ribeiro",
84+
"userId": "15488710231554262191"
85+
}
86+
},
87+
"outputId": "778487c5-b0b5-4a7f-a024-33122e49b6fb",
88+
"trusted": true
89+
},
90+
"outputs": [],
91+
"execution_count": null
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"source": "Let's create a dummy workspace and project as base for our users management example.\n\nChange the variable `WORKSPACE`with your desired workspace name and `PROJECT` with project name.",
96+
"metadata": {
97+
"id": "gFN3jXIjntwf"
98+
}
99+
},
100+
{
101+
"cell_type": "code",
102+
"source": "# Add here your existing workspace name and the new project name\nWORKSPACE=\"\"\nPROJECT=\"\"\n\n# Create new workspace\n#INFO: Only uncomment if you are able to create a new workspace. Mergin Maps free tier only allows for 1 workspace per user. In this case use your existing workspace.\n#client.create_workspace(WORKSPACE) \n\n# Create new dummy project (public accessible)\nclient.create_project(project_name=PROJECT, namespace=WORKSPACE, is_public=True)",
103+
"metadata": {
104+
"id": "27rA4VfgoJjy",
105+
"executionInfo": {
106+
"status": "ok",
107+
"timestamp": 1748364795430,
108+
"user_tz": -60,
109+
"elapsed": 745,
110+
"user": {
111+
"displayName": "Fernando Ribeiro",
112+
"userId": "15488710231554262191"
113+
}
114+
}
115+
},
116+
"outputs": [],
117+
"execution_count": 3
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"source": "Create some users on your Mergin Maps example workspace from the provided example file in `01_users_assets/users.csv` with random permissions.",
122+
"metadata": {
123+
"id": "SXimIDIDqb9J"
124+
}
125+
},
126+
{
127+
"cell_type": "code",
128+
"source": "import csv\nimport string\nimport random\n\nfrom mergin.common import WorkspaceRole\n\npermissions = [WorkspaceRole.EDITOR,\n WorkspaceRole.READER,\n WorkspaceRole.WRITER]\n\n\ntry:\n with open('01_users_assets/users.csv', mode='r', newline='', encoding='utf-8') as csvfile:\n reader = csv.reader(csvfile)\n header = next(reader) # Skip header\n first_data_row = next(reader) # Get first data row\n if len(first_data_row) >= 2: # Check if there are at least two columns\n username = first_data_row[0]\n email = first_data_row[1]\n # add new mergin maps user\n client.create_user(username=username, password=random.choices(string.ascii_uppercase + string.digits, k=12), email=email, workspace_id=WORKSPACE, workspace_role=random.choice(permissions))\n print(f\"User '{username}' created successfully.\")\n else:\n print(\"First data row does not have enough columns.\")\nexcept FileNotFoundError:\n print(f\"File '{filename}' not found for processing example.\")\nexcept StopIteration:\n print(f\"File '{filename}' does not contain enough data rows for processing example.\")\nexcept Exception as e:\n print(f\"An error occurred during processing example: {e}\")\n",
129+
"metadata": {
130+
"id": "Lp351dFYquVs"
131+
},
132+
"outputs": [],
133+
"execution_count": null
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"source": "Let's change permission level for a specific user to `READER`.",
138+
"metadata": {
139+
"id": "OoCFI9u7uClQ"
140+
}
141+
},
142+
{
143+
"cell_type": "code",
144+
"source": "client.update_workspace_member(username='agreen', workspace_id=WORKSPACE, workspace_role=WorkspaceRole.READER)",
145+
"metadata": {
146+
"id": "6QG8Smj2uI2l"
147+
},
148+
"outputs": [],
149+
"execution_count": null
150+
},
151+
{
152+
"cell_type": "markdown",
153+
"source": "Remove the same user from our dummy example project",
154+
"metadata": {
155+
"id": "PpSxZfRfujj7"
156+
}
157+
},
158+
{
159+
"cell_type": "code",
160+
"source": "client.remove_project_collaborator(project_path='{workspace}/{project}'.format(workspace=WORKSPACE, project=PROJECT), username='agreen')",
161+
"metadata": {
162+
"id": "5_7lhNhVuukI"
163+
},
164+
"outputs": [],
165+
"execution_count": null
166+
},
167+
{
168+
"cell_type": "markdown",
169+
"source": "Remove a user from an workspace",
170+
"metadata": {}
171+
},
172+
{
173+
"cell_type": "code",
174+
"source": "client.remove_workspace_member(username='agreen', workspace_id=WORKSPACE)",
175+
"metadata": {
176+
"trusted": true
177+
},
178+
"outputs": [],
179+
"execution_count": null
180+
}
181+
]
182+
}

examples/01_users_assets/users.csv

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
username,email
2+
agreen,agreen@example.com
3+
bsmith,bsmith@example.com
4+
cjones,cjones@example.com
5+
dwilliams,dwilliams@example.com
6+
ebrown,ebrown@example.com
7+
fdavis,fdavis@example.com
8+
gmiller,gmiller@example.com
9+
hwilson,hwilson@example.com
10+
imoore,imoore@example.com
11+
jtaylor,jtaylor@example.com
12+
kanderson,kanderson@example.com
13+
lthomas,lthomas@example.com
14+
mjackson,mjackson@example.com
15+
nwhite,nwhite@example.com
16+
oharris,oharris@example.com
17+
pmartin,pmartin@example.com
18+
qthompson,qthompson@example.com
19+
rgarcia,rgarcia@example.com
20+
smartinez,smartinez@example.com
21+
trobinson,trobinson@example.com

0 commit comments

Comments
 (0)