Hi i am trying to update user role in a particular...
# permit-saas
g
Hi i am trying to update user role in a particular tenant where i am removing and adding the role. But permit is not updating the roles according.
a
Hi @Girija shankar, I'm an AI assistant for Permit.io. I'll analyze available sources and get back to you shortly.
When updating user roles in a tenant, you need to properly unassign the old role before assigning the new one. Here's how to do it correctly: For ​*Node.js*​, first unassign the role (1) :
Copy code
const response = await permit.api.unassignRole(JSON.stringify(unassignedRole) ) ;
Where the
unassignedRole
object should contain :
Copy code
{  
 role: "role",  
 tenant: "tenant",  
 user: "user"  
}
Then assign the new role (2) :
Copy code
await permit.api.assignRole({
 role: "Manager",
 tenant: "Marketing",
 user: "user|123456789",
}) ;
(3) For ​*Golang*​, use (4) :
Copy code
user, err := Permit.Api.Users.UnasignRole(ctx, "user-key", "role-key", "tenant-key")
For ​*Ruby*​, you can use the unassign_role endpoint (5) :
Copy code
api_instance.unassign_role(proj_id, env_id, role_assignment_remove)
To help our engineers investigate why the roles aren't updating properly: 1. Which Workspace / Project / Env are you using? 2. What calls are you performing (as SDK code or CURL) ? 3. Which PDP version are you running? 4. Could you share relevant logs from your PDP?
g
Currently we are doing it first removing all assigned role
Copy code
String tenant = resolveTenant(organizationId);

UserRead user = usersApi.get(userId);

RoleAssignmentRead[] assignedRoles = usersApi.getAssignedRoles(user.key);

for (RoleAssignmentRead assignedRole : assignedRoles) {
    if (tenant.equals(assignedRole.tenant)) {
       
        roleAssignmentsApi.unassign(
                new RoleAssignmentRemove(assignedRole.role, assignedRole.user)
                        .withTenant(tenant)
        );
    }
}
and then assigning the new one
Copy code
String tenant = resolveTenant(organizationId);
UserRead user = usersApi.get(userId);

roleAssignmentsApi.assign(
        new RoleAssignmentCreate(AccountRole.OWNER.getValue(), user.key)
                .withTenant(tenant));
according to this logic user should have now only "Owner" as a role but permit is showing both role the previous one and the latest one "Owner" and "User"
o
@Ask Inkeep
a
Thanks for tagging me @Or Weis. I'll look through relevant sources and get back to you shortly.
o
We'll be with you shortly
a
Based on the Java code you've shared, the issue appears to be with the role assignment persistence. Looking at the documentation, when assigning roles to users, the proper approach is to use the role assignment endpoints correctly. (1) (2) For Java, based on the pattern shown in the documentation, you should ensure you're using the correct API endpoints. The Ruby documentation shows a similar pattern for role assignments: (3)
Copy code
role_assignment_create = OpenapiClient::RoleAssignmentCreate.new({role: 'role_example', tenant: 'tenant_example', user: 'user_example'})
To help our engineers investigate why roles aren't being properly updated: 1. Which Workspace / Project / Env are you using? 2. What exact SDK version and calls are you performing (please share the full code including imports) ? 3. Which PDP version are you running? 4. Could you share relevant logs from your PDP showing the unassign and assign operations? Additionally, please verify: • Are you checking the roles immediately after the operations, or is there a delay? • Can you confirm the unassign operation is returning successfully (check the response status) ? • Are there any error messages in your PDP logs during these operations? This information will help identify if this is a timing issue, a caching problem, or a potential bug in the role assignment flow.
o
If the problem you're experience is a race condition please be sure to use the consistent updates feature https://docs.permit.io/how-to/manage-data/local-facts-uploader/
t
Hey Girija, let me try to reproduce this and I'll get back with you.
g
Sure @Thomas thanks
This seems to be an issue when we are calling APIs too frequently.
t
Hey @Girija shankar can you provide the script? I think the role removal isn't completing before you start the add role. I think it's a timing thing on touching the object. I don't think consistent updates will help here. I think it's a timing thing on the API.
Can you add in an additional API call to check the role assignments after the delete call? It'd be good to log the users that don't get their roles removed successfully so we can investigate this further.
a
Hey @Girija shankar, just bumping this message so we can handle it.
g
Hey @Thomas , @Alon Boshi yes its the timing thing i introduced Thread.sleep for 1 second and it worked.
Thanks for you time and quick response
t
@Girija shankar that's good to hear and does suggest a timing issue with the API. Would you be willing to provide the full example code you ran so that we can try and replicate it on our end? Although the 1s wait is a good work around, our APIs should handle the calls you sent like you're trying to use them.