In my first story (Manipulate Azure AD B2C Data), I explained the basic setup of Prisma for PostgreSQL (I used the free plan from Neon.tech) and how you can update your User Objects within the Azure AD Object Store. We will examine how you can further manipulate your data in both sources.
Here is the GitHub repo with the full code.
In my previous story, regarding CRUD operations on Azure AD B2C, I explained how you can “Patch” (modify) data attributes of your User Objects and how you can create a new user record with the famous ORM Prisma in your PostgreSQL DB (I used the free offering of neon.tech).
Below is the Next.js code snippet for storing a new user in the “roles” table with Prisma. We pass only one attribute, “name”.
Prisma Schema
Here is the model definition for the roles tables in my “schema.prisma” file
And here is the complete schema.prisma file, with all the tables that we defined in the last stories
Prisma Delete Record
To delete this new record, I use this code snippet.
Here is the complete code from the component “promoteUser.component.tsx”, where I show the Users from Azure AD B2C directory and on each line a “Select” for the roles, which we defined via Prisma in our “roles” table and one button for the role change and a new button for “User Delete”. I am using the Table component from Tremor/React as a base structure and three different API calls for CRUD operations
API for Azure AD B2C User Deletion
If we have a valid Access Token in our ApiToken table, we use that one. Otherwise, we generate and save a new one. We can perform the delete call with the Token and the Azure AD B2C ID of the user object.
Needed Azure AD B2C permissions for User deletion
We are using the service principal (app registration) “TestProd”, which I showed in this story.
However, the permission for user deletion is not given, so we have to adapt them.
To fix the issue, you need to assign your service principal to a directory role e.g. User administrator/ Global administrator.
Navigate to the Azure Active Directory in the Azure portal -> Roles and administrators -> click User administrator or Global administrator -> Add assignment -> search by your service principal name(must search) -> find it and select it -> click Select.
API for removing Role Records with Prisma
We pass the UserID from our Component, which we then use to delete the corresponding record from the Roles Table with Prisma.
Component for handling the roles in our Dashboard section
Below, you will find the component that we use to list the created roles. The component displays a “Delete” button on every row so that you can easily delete it.
Cloudapp-dev, and before you leave us
Thank you for reading until the end. Before you go: