-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[BEEEP] Strongly typed OrganizationUser objects #6794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
eliykat
wants to merge
7
commits into
main
Choose a base branch
from
strongly-typed-orgusers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+692
โ13
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4315f5a
first pass
eliykat 1d42b33
Add mapping methods
eliykat 8de49f7
Handle revoked the new way
eliykat f96dba4
Example query
eliykat 53e4ab8
Use interface instead of oneof
eliykat 03dc146
dotnet format
eliykat 718019e
Example command usage
eliykat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/GetOrganizationUserQuery.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| ๏ปฟusing Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces; | ||
| using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Models; | ||
| using Bit.Core.Entities; | ||
| using Bit.Core.Enums; | ||
| using Bit.Core.Repositories; | ||
| using Bit.Core.Services; | ||
|
|
||
| namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers; | ||
|
|
||
| public class GetOrganizationUserQuery(IOrganizationUserRepository organizationUserRepository) | ||
| : IGetOrganizationUserQuery | ||
| { | ||
| public async Task<ITypedOrganizationUser?> GetOrganizationUserAsync(Guid organizationUserId) | ||
| { | ||
| var organizationUser = await organizationUserRepository.GetByIdAsync(organizationUserId); | ||
|
|
||
| if (organizationUser == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return ConvertToStronglyTypedModel(organizationUser); | ||
| } | ||
|
|
||
| public async Task<IEnumerable<ITypedOrganizationUser>> GetManyOrganizationUsersAsync(IEnumerable<Guid> organizationUserIds) | ||
| { | ||
| var organizationUsers = await organizationUserRepository.GetManyAsync(organizationUserIds); | ||
|
|
||
| return organizationUsers | ||
| .Select(ConvertToStronglyTypedModel) | ||
| .ToList(); | ||
| } | ||
|
|
||
| private static ITypedOrganizationUser ConvertToStronglyTypedModel(OrganizationUser organizationUser) | ||
| { | ||
| // Determine the appropriate model type based on the status | ||
| // For revoked users, use GetPriorActiveOrganizationUserStatusType to determine the underlying status | ||
| var effectiveStatus = organizationUser.Status == OrganizationUserStatusType.Revoked | ||
| ? OrganizationService.GetPriorActiveOrganizationUserStatusType(organizationUser) | ||
| : organizationUser.Status; | ||
|
|
||
| return effectiveStatus switch | ||
| { | ||
| OrganizationUserStatusType.Invited => InvitedOrganizationUser.FromEntity(organizationUser), | ||
| OrganizationUserStatusType.Accepted => AcceptedOrganizationUser.FromEntity(organizationUser), | ||
| OrganizationUserStatusType.Confirmed => ConfirmedOrganizationUser.FromEntity(organizationUser), | ||
| _ => throw new InvalidOperationException($"Unsupported organization user status: {organizationUser.Status}") | ||
| }; | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
...minConsole/OrganizationFeatures/OrganizationUsers/Interfaces/IGetOrganizationUserQuery.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| ๏ปฟnamespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces; | ||
|
|
||
| public interface IGetOrganizationUserQuery | ||
| { | ||
| /// <summary> | ||
| /// Retrieves an organization user by their ID and returns the appropriate strongly-typed model | ||
| /// based on their status (Invited, Accepted, Confirmed, or Revoked). | ||
| /// </summary> | ||
| /// <param name="organizationUserId">The ID of the organization user to retrieve.</param> | ||
| Task<ITypedOrganizationUser?> GetOrganizationUserAsync(Guid organizationUserId); | ||
|
|
||
| /// <summary> | ||
| /// Retrieves multiple organization users by their IDs and returns the appropriate strongly-typed models | ||
| /// based on their status (Invited, Accepted, Confirmed, or Revoked). | ||
| /// </summary> | ||
| /// <param name="organizationUserIds">The IDs of the organization users to retrieve.</param> | ||
| Task<IEnumerable<ITypedOrganizationUser>> GetManyOrganizationUsersAsync(IEnumerable<Guid> organizationUserIds); | ||
| } |
51 changes: 51 additions & 0 deletions
51
...Console/OrganizationFeatures/OrganizationUsers/Interfaces/IOrganizationUserPermissions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| ๏ปฟ#nullable enable | ||
|
|
||
| using Bit.Core.Models.Data; | ||
| using Bit.Core.Utilities; | ||
|
|
||
| namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Represents an entity that has organization user permissions stored as a JSON string. | ||
| /// </summary> | ||
| public interface IOrganizationUserPermissions | ||
| { | ||
| /// <summary> | ||
| /// A json blob representing the <see cref="Bit.Core.Models.Data.Permissions"/> of the OrganizationUser if they | ||
| /// are a Custom user role. MAY be NULL if they are not a custom user, but this is not guaranteed; | ||
| /// do not use this to determine their role. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Avoid using this property directly - instead use the extension methods | ||
| /// <see cref="OrganizationUserPermissionsExtensions.GetPermissions"/> and | ||
| /// <see cref="OrganizationUserPermissionsExtensions.SetPermissions"/>. | ||
| /// </remarks> | ||
| string? Permissions { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for working with <see cref="IOrganizationUserPermissions"/> implementations. | ||
| /// </summary> | ||
| public static class OrganizationUserPermissionsExtensions | ||
| { | ||
| /// <summary> | ||
| /// Deserializes the Permissions JSON string into a <see cref="Permissions"/> object. | ||
| /// </summary> | ||
| /// <param name="organizationUser">The organization user with permissions.</param> | ||
| /// <returns>A <see cref="Permissions"/> object if the JSON is valid, otherwise null.</returns> | ||
| public static Permissions? GetPermissions(this IOrganizationUserPermissions organizationUser) | ||
| { | ||
| return string.IsNullOrWhiteSpace(organizationUser.Permissions) ? null | ||
| : CoreHelpers.LoadClassFromJsonData<Permissions>(organizationUser.Permissions); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Serializes a <see cref="Permissions"/> object into a JSON string and stores it. | ||
| /// </summary> | ||
| /// <param name="organizationUser">The organization user to set permissions on.</param> | ||
| /// <param name="permissions">The permissions object to serialize.</param> | ||
| public static void SetPermissions(this IOrganizationUserPermissions organizationUser, Permissions permissions) | ||
| { | ||
| organizationUser.Permissions = CoreHelpers.ClassToJsonData(permissions); | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
.../AdminConsole/OrganizationFeatures/OrganizationUsers/Interfaces/ITypedOrganizationUser.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| ๏ปฟusing Bit.Core.Entities; | ||
| using Bit.Core.Enums; | ||
| using Bit.Core.Models; | ||
|
|
||
| namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Represents common properties shared by all typed organization user models. | ||
| /// </summary> | ||
| public interface ITypedOrganizationUser : IExternal, IOrganizationUserPermissions | ||
| { | ||
| /// <summary> | ||
| /// A unique identifier for the organization user. | ||
| /// </summary> | ||
| Guid Id { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The ID of the Organization. | ||
| /// </summary> | ||
| Guid OrganizationId { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The User's role in the Organization. | ||
| /// </summary> | ||
| OrganizationUserType Type { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The date the OrganizationUser was created. | ||
| /// </summary> | ||
| DateTime CreationDate { get; } | ||
|
|
||
| /// <summary> | ||
| /// The last date the OrganizationUser entry was updated. | ||
| /// </summary> | ||
| DateTime RevisionDate { get; } | ||
|
|
||
| /// <summary> | ||
| /// True if the User has access to Secrets Manager for this Organization, false otherwise. | ||
| /// </summary> | ||
| bool AccessSecretsManager { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// True if the user's access has been revoked, false otherwise. | ||
| /// </summary> | ||
| bool Revoked { get; set; } | ||
|
|
||
| public OrganizationUser ToEntity(); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be made public in order to be used in places outside this query? Especially in places where we might need these types in other queries that return Org users.