Skip to content

Commit ff3280f

Browse files
committed
reviewed the Switching Between Organization Units blog post
1 parent 41dabea commit ff3280f

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

48 KB
Loading

blog-posts/en/switching-between-organization-units.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
# Switching Between Organization Units
22

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.
44

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).
66

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;
8+
9+
![image](./images/Blog/switch-ou-sample-screebshot.jpg)
10+
11+
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.
816

917
### Creating Claims Principal for Organization Unit
1018

@@ -25,7 +33,7 @@ public override async Task<ClaimsPrincipal> CreateAsync(User user)
2533
}
2634
```
2735

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.
2937

3038
Within this context, we add the first element of the Organization Unit as a claim during user principal creation.
3139

@@ -39,11 +47,13 @@ private async Task<User> GetUserWithOrganizationUnitsAsync(long userId)
3947
}
4048
```
4149

42-
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.
4353

4454
### Modifying the Organization Unit in Session
4555

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).
4757

4858
```csharp
4959
public class MyAppSession : ClaimsAbpSession, ITransientDependency
@@ -93,7 +103,7 @@ With these additions, the `MyAppSession` class now includes methods to both retr
93103

94104
### Usage Example: Switching Organization Units
95105

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.
97107

98108
```csharp
99109
public class OrganizationUnitManager
@@ -125,12 +135,16 @@ public class OrganizationUnitManager
125135

126136
#### Usage Steps
127137

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.
132139

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;
133143

134-
## Conclusion
144+
```csharp
145+
public async Task ChangeOrganizationUnit(UserOrganizationUnit newUserOrganizationUnit){
146+
await _organizationUnitManager.SwitchOrganizationUnitAsync(newUserOrganizationUnit);
147+
}
148+
```
135149

136-
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

Comments
 (0)