@@ -92,3 +92,156 @@ examples and not recommendations.
9292
9393You have now made the Product visible, and optionally editable, by DejaCode Users
9494that are not superusers.
95+
96+ 4. Manage Product Object Permissions via the REST API
97+ -----------------------------------------------------
98+
99+ Product object permissions can also be managed programmatically through the REST API.
100+ This is especially useful for CI/CD pipelines that create Product versions automatically
101+ and need to assign permissions without manual intervention.
102+
103+ The endpoint is available at::
104+
105+ /api/v2/products/{uuid}/permissions/
106+
107+ **Authentication **
108+
109+ All requests require authentication. The examples below use an API key passed via
110+ the ``Authorization `` header::
111+
112+ Authorization: Token <your-api-token>
113+
114+ **Available permissions **
115+
116+ The following permission codenames can be assigned to users or groups:
117+
118+ - ``view_product `` -- allows viewing the product
119+ - ``change_product `` -- allows editing the product
120+ - ``delete_product `` -- allows deleting the product
121+
122+ **Finding the Product UUID **
123+
124+ Retrieve the UUID from the product list endpoint::
125+
126+ GET /api/v2/products/?name=MyApp&version=2.0
127+
128+ The ``uuid `` field is included in each product entry of the response.
129+
130+ 4.1 List current permissions
131+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
132+
133+ Retrieve all users and groups that currently have permissions on a product::
134+
135+ GET /api/v2/products/{uuid}/permissions/
136+
137+ Response::
138+
139+ {
140+ "users": [
141+ {
142+ "dataspace": "nexB",
143+ "username": "alice",
144+ "object_permissions": ["view_product", "change_product"]
145+ }
146+ ],
147+ "groups": [
148+ {
149+ "name": "backend-team",
150+ "object_permissions": ["view_product"]
151+ }
152+ ]
153+ }
154+
155+ 4.2 Assign permissions to a user
156+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
157+
158+ Provide ``user `` (username) and a ``permissions `` list::
159+
160+ POST /api/v2/products/{uuid}/permissions/
161+ Content-Type: application/json
162+
163+ {
164+ "user": "alice",
165+ "permissions": ["view_product", "change_product"]
166+ }
167+
168+ Successful response::
169+
170+ {"status": "permissions assigned"}
171+
172+ 4.3 Assign permissions to a group
173+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
174+
175+ Use ``group `` (group name) instead of ``user ``. All members of the group will
176+ inherit the assigned permissions::
177+
178+ POST /api/v2/products/{uuid}/permissions/
179+ Content-Type: application/json
180+
181+ {
182+ "group": "backend-team",
183+ "permissions": ["view_product"]
184+ }
185+
186+ This is the recommended approach when multiple users need access to the same set
187+ of products. Manage group membership via the DejaCode admin, then assign the group
188+ to each product once.
189+
190+ 4.4 Remove permissions from a user or group
191+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
192+
193+ Use the ``DELETE `` method with the same body format::
194+
195+ DELETE /api/v2/products/{uuid}/permissions/
196+ Content-Type: application/json
197+
198+ {
199+ "user": "alice",
200+ "permissions": ["change_product"]
201+ }
202+
203+ Or for a group::
204+
205+ DELETE /api/v2/products/{uuid}/permissions/
206+ Content-Type: application/json
207+
208+ {
209+ "group": "backend-team",
210+ "permissions": ["view_product"]
211+ }
212+
213+ Successful response::
214+
215+ {"status": "permissions removed"}
216+
217+ 4.5 Automate permissions in a CI/CD pipeline
218+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
219+
220+ The following shell script illustrates how to create a Product version and immediately
221+ assign permissions to a group, so that team members can view it without any manual
222+ step::
223+
224+ BASE_URL="https://dejacode.example.com/api/v2"
225+ TOKEN="your-api-token"
226+ GROUP="backend-team"
227+
228+ # Create the product version
229+ RESPONSE=$(curl -s -X POST "$BASE_URL/products/" \
230+ -H "Authorization: Token $TOKEN" \
231+ -H "Content-Type: application/json" \
232+ -d '{"name": "MyApp", "version": "3.0"}')
233+
234+ UUID=$(echo "$RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin)['uuid'])")
235+
236+ # Assign view permission to the team
237+ curl -s -X POST "$BASE_URL/products/$UUID/permissions/" \
238+ -H "Authorization: Token $TOKEN" \
239+ -H "Content-Type: application/json" \
240+ -d "{\"group\": \"$GROUP\", \"permissions\": [\"view_product\"]}"
241+
242+ **Access control for the permissions endpoint **
243+
244+ Only the following users can call the ``/permissions/ `` endpoint on a given product:
245+
246+ - A **superuser **
247+ - The user who **created ** the product (``created_by `` field)
0 commit comments