You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog-posts/en/switching-between-organization-units.md
+27-13Lines changed: 27 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,18 @@
1
1
# Switching Between Organization Units
2
2
3
-
In ASP.NET Zero projects, the ability for users to switch between different organizational units is essential, especially for multi unit organizations.Allowing users to select and change their active unit during their sessions enhances both the user experience and operational efficiency. However, managing this transition correctly and securely can often be complex.
3
+
In most companies, a user belongs to more than one organization. Also, in some applications, we need to filter the data shown depending on the logged-in user's organization. For such scenarios, allowing users to select one of the organizations they belong to is a good practice.
4
4
5
-
## Implementing of Organization Unit Switching Between
5
+
For creating a custom data filter, you can check [https://aspnetboilerplate.com/Pages/Documents/Articles%5CHow-To%5Cadd-custom-data-filter-ef-core](https://aspnetboilerplate.com/Pages/Documents/Articles%5CHow-To%5Cadd-custom-data-filter-ef-core).
6
6
7
-
In this section, we'll delve into the implementation details of enabling users to switch between different organizational units within an ASP.NET Zero project.
7
+
In order to allow users to change their active organization unit, we can design a UI like the one below;
By using this dropdown, a user can switch between organization units.
12
+
13
+
## Implementing Organization Unit Switching
14
+
15
+
In this section, we'll dive into the implementation details of enabling users to switch between different organizational units within an ASP.NET Zero project.
8
16
9
17
### Creating Claims Principal for Organization Unit
10
18
@@ -25,7 +33,7 @@ public override async Task<ClaimsPrincipal> CreateAsync(User user)
25
33
}
26
34
```
27
35
28
-
The first code snippet overrides the **CreateAsync** method to incorporate organization unit information into the claims principal.
36
+
We are overwriting the **CreateAsync** method of `UserClaimsPrincipalFactory`to store user's selected organization unit in claims principal.
29
37
30
38
Within this context, we add the first element of the Organization Unit as a claim during user principal creation.
The second snippet demonstrates the asynchronous method GetUserWithOrganizationUnitsAsync, which retrieves a user with their associated organization units from the database.
50
+
Private `GetUserWithOrganizationUnitsAsync` method retrieves a user with user's associated organization units from the database.
51
+
52
+
Now, we can use `Organization_Unit` claim as the currently selected organization unit for logged-in user and filter any data using its value.
43
53
44
54
### Modifying the Organization Unit in Session
45
55
46
-
Here, we implement functionality to modify the organizational unit within the session, following the guidelines outlined in this [document](https://aspnetboilerplate.com/Pages/Documents/Articles%5CHow-To%5Cadd-custom-session-field-aspnet-core).
56
+
Now, we must allow logged-in user to switch selected organization unit. To do that, we first need to add a new field to `AbpSession` by following this [document](https://aspnetboilerplate.com/Pages/Documents/Articles%5CHow-To%5Cadd-custom-session-field-aspnet-core).
@@ -93,7 +103,7 @@ With these additions, the `MyAppSession` class now includes methods to both retr
93
103
94
104
### Usage Example: Switching Organization Units
95
105
96
-
In this section, we will demonstrate how to use a method for switching a user's organization unit within a new service class. The following example shows the `OrganizationUnitManager` class, which uses `MyAppSession` to allow a user to be removed from their current organization unit and added to a new one.
106
+
In this section, we will demonstrate how to use a method for switching a user's organization unit within a new service class. The following example shows the `OrganizationUnitManager` class, which uses `MyAppSession` to allow a user to change currently selected organization unit.
97
107
98
108
```csharp
99
109
publicclassOrganizationUnitManager
@@ -125,12 +135,16 @@ public class OrganizationUnitManager
125
135
126
136
#### Usage Steps
127
137
128
-
1.**Retrieve the Current Organization Unit:** The oldOrganizationUnit is deserialized from the current session's OrganizationUnit claim.
129
-
2.**Set the New Organization Unit:** The SetOrganizationUnit method of MyAppSession is called to set the new organization unit in the session.
130
-
3.**Remove from the Old Unit:** The user is removed from the old organization unit using _userManager.RemoveFromOrganizationUnitAsync.
131
-
4.**Add to the New Unit:** The user is added to the new organization unit using _userManager.AddToOrganizationUnitAsync.
138
+
So, we completed the backend side to switch user's current organization unit in claims principal. So, let's see how we can use it from the client side.
132
139
140
+
1.**Fill the user's organization units**: As shown in the image above in the introcution section, we need to fill the user's organization units to a dropdown. To do this, you can use `_userManager.GetOrganizationUnitsAsync(user)` and map its result to a DTO class and return it to client and fill the dropdown using returned list.
141
+
2.**Retrieve the Current Organization Unit:** The oldOrganizationUnit is deserialized from the current session's OrganizationUnit claim. So, you can use this value to set selected value of the organization unit dropdown.
142
+
3.**Set the New Organization Unit:** On the change event of the dropdown, call an API endpoint (This is not explained above but you can use ProfileAppService and create a new method in this application service class) and this endpoint should call;
Switching between organizational units in ASP.NET Zero projects can increase your organization's flexibility in transitions between organizations and facilitate organizational management. With a custom session, it is possible to switch between the user's current organization information and the organization. This work can also simplify organizational operational processes.
150
+
So, that's all and our app allows users to change their active organization unit on the UI.
0 commit comments