Impossible to add user permission to a grafana dashboard

Hello Grafana community,

I’m using Grafana 10.2 version on linux and I trying to create a python program to authorize a user to view a dashboard. Seem simple, but facing difficulties since many days.

Today, I can import a dashboard via the HTTP API and I can create the user, but impossible to add permission to the user the view the imported dashboard.

I follow the API reference guide and today the program finish with success but all permissions are removed except the Admin.

see the pictures of the permissions, before and after my program.

here below my program:

from requests.auth import HTTPBasicAuth
import json
import subprocess

grafana_url_api = 'http://192.168.YYY.XXX:3000/api'

auth = ('admin', 'zzzzzzzz')

grafana_token = "XXXXXXXXXXXXXXXXXXXXXXXX"

router = "util7"

url= grafana_url_api + "/users/lookup?loginOrEmail=" + router

response = requests.get(url, auth=auth)

if response.status_code == 200:
   myuser_data = response.json()
   user_uid = myuser_data.get("id")
   print(f"User UID for user {router}: {user_uid}")
else:
   print(f"Failed to retrieve user UID. Status Code: {response.status_code}, Response: {response.text}")

 *#provide permission to user to dashboard*
myheader = {
      'Authorization': f"Bearer {grafana_token}",
      'Content-Type': 'application/json',
      'Accept': 'application/json',
}

url = grafana_url_api + f'/dashboards/uid/{router}-R/permissions'
print(url) 

  #Get the current dashboard permissions*
response = requests.get(url, headers=myheader)

if response.status_code == 200:
   dashboard_data = response.json()
   print(dashboard_data)
 
   new_user_entry = {
      'userId': user_uid,
      'permission': 1,
   }

   update_response = requests.post(url, headers=myheader, json=new_user_entry)

   if update_response.status_code == 200:
       print(f'Permission for {router} updated successfully to Viewer')
   else:
       print(f'Failed to update permission: {update_response.text}')
else:
      print(f'Failed to fetch dashboard data: {response.text}')

Do you have any glue to solve the issue?

I really need help on this. Thanks all of you.

Hi,

I see your json payload only includes the new permissions you wish to set, the documentation is pretty clear that if you leave any of the existing permissions out of your payload, they will be removed…

Yes, I understand that the viewer and editor role have been removed because I have not put them in the json (it is what I want indeed). But my user is not added.

Even, when I add the “admin” user I got the same result: The user is not added to the permissions to the dashboard.

Cheers,

Laurent

General recommendation: use UI and set permission via UI + watch API calls in the browser console and then just replicate them in your code. Print also API response status code, not only response text in your code. And of course read them.

You complain that your code is not working, but you didn’t show your response status codes, text when it is not working.

And please format code, texts when you are posting them here.

here the message from the API response:

Status Code: 200, Response: {“message”:“Dashboard permissions updated”}
{‘message’: ‘Dashboard permissions updated’}

So API request is correct. Payload is probably wrong. As I mentioned: use browser console and construct right payload.

Thank you for your help, the idea to use the browser console was a great idea.

now it is working so I give the solution for others:

auth3 = (‘xxxx’, ‘yyyyy’)

mytext = {“permission”:“View”}

url = grafana_url_api + f’/access-control/dashboards/{dash_uuid}/users/{user_uid}’

response = requests.post(url, auth=auth3, json=mytext)
print(f"Status Code: {response.status_code}, Response: {response.text}")

in fact, my previous url and json were wrong. So you need the user uid and the dashboard uid to update the permissions

Solved !!

2 Likes