-
Notifications
You must be signed in to change notification settings - Fork 72
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
feat(dashboard): Improve validation for missing permissions when creating new app #14395
Open
framitdavid
wants to merge
12
commits into
main
Choose a base branch
from
feat/verifyAccessToCreatOrgFrontend
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.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e3d1745
feat(desinger): endpoint to get UserRepositoryPermissions
framitdavid 698e334
unit-test for UserService
framitdavid 29c8438
dotnet format
framitdavid dc033af
added unit-test
framitdavid 2f82f37
and operator
framitdavid 907ab42
improve naming
framitdavid bc14a2f
feat(dashboard): Improve error handling for missing permissions when …
framitdavid e8b3cfb
work in progress for unit-testing
framitdavid 850d2eb
testing
framitdavid 78f3607
added few more test cases
framitdavid 1d699c2
make props argument nullable
framitdavid 92ff394
typecheck errors
framitdavid 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 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
This file contains 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
This file contains 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,6 @@ | ||
namespace Altinn.Studio.Designer.Models.Dto; | ||
|
||
public class UserOrgPermission | ||
{ | ||
public bool CanCreateOrgRepo { get; set; } | ||
} |
This file contains 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
44 changes: 44 additions & 0 deletions
44
backend/src/Designer/Services/Implementation/UserService.cs
This file contains 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,44 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Altinn.Studio.Designer.Helpers; | ||
using Altinn.Studio.Designer.Models.Dto; | ||
using Altinn.Studio.Designer.RepositoryClient.Model; | ||
using Altinn.Studio.Designer.Services.Interfaces; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Altinn.Studio.Designer.Services.Implementation; | ||
|
||
public class UserService : IUserService | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly IGitea _giteaApi; | ||
|
||
public UserService(IHttpContextAccessor httpContextAccessor, IGitea giteaApi) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
_giteaApi = giteaApi; | ||
} | ||
|
||
public async Task<UserOrgPermission> GetUserOrgPermission(string org) | ||
{ | ||
bool canCreateOrgRepo = await HasPermissionToCreateOrgRepo(org); | ||
return new UserOrgPermission() { CanCreateOrgRepo = canCreateOrgRepo }; | ||
} | ||
|
||
private bool IsUserSelfOrg(string org) | ||
{ | ||
return AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext) == org; | ||
} | ||
|
||
private async Task<bool> HasPermissionToCreateOrgRepo(string org) | ||
{ | ||
List<Team> teams = await _giteaApi.GetTeams(); | ||
return IsUserSelfOrg(org) || teams.Any(team => CheckPermissionToCreateOrgRepo(team, org)); | ||
} | ||
|
||
private static bool CheckPermissionToCreateOrgRepo(Team team, string org) | ||
{ | ||
return team.can_create_org_repo && team.Organization.Username == org; | ||
} | ||
} |
This file contains 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,9 @@ | ||
using System.Threading.Tasks; | ||
using Altinn.Studio.Designer.Models.Dto; | ||
|
||
namespace Altinn.Studio.Designer.Services.Interfaces; | ||
|
||
public interface IUserService | ||
{ | ||
public Task<UserOrgPermission> GetUserOrgPermission(string org); | ||
} |
This file contains 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
This file contains 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,49 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Altinn.Studio.Designer.RepositoryClient.Model; | ||
using Altinn.Studio.Designer.Services.Implementation; | ||
using Altinn.Studio.Designer.Services.Interfaces; | ||
using Microsoft.AspNetCore.Http; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Designer.Tests.Services | ||
{ | ||
public class UserServiceTests | ||
{ | ||
private readonly Mock<IHttpContextAccessor> _httpContextAccessor; | ||
private readonly Mock<IGitea> _giteaApi; | ||
|
||
public UserServiceTests() | ||
{ | ||
_httpContextAccessor = new Mock<IHttpContextAccessor>(); | ||
_giteaApi = new Mock<IGitea>(); | ||
var context = new DefaultHttpContext(); | ||
_httpContextAccessor.Setup(req => req.HttpContext).Returns(context); | ||
} | ||
|
||
[Theory] | ||
[InlineData("org1", false)] | ||
[InlineData("org2", true)] | ||
public async Task GetUserOrgPermission_ReturnsCorrectPermission(string org, bool expectedCanCreate) | ||
{ | ||
var teams = new List<Team> | ||
{ | ||
new Team | ||
{ | ||
Organization = new Organization { Username = org }, | ||
can_create_org_repo = expectedCanCreate | ||
} | ||
}; | ||
|
||
_giteaApi.Setup(api => api.GetTeams()).ReturnsAsync(teams); | ||
|
||
var userService = new UserService(_httpContextAccessor.Object, _giteaApi.Object); | ||
|
||
var result = await userService.GetUserOrgPermission(org); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal(expectedCanCreate, result.CanCreateOrgRepo); | ||
} | ||
} | ||
} |
This file contains 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
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.
All backend code should be reviewed in another PR: #14389