Skip to content
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

Merged
merged 20 commits into from
Jan 27, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(desinger): endpoint to get UserRepositoryPermissions
framitdavid committed Jan 9, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit e3d174521cd988358f4acbe28eaf2e161dafe850
13 changes: 12 additions & 1 deletion backend/src/Designer/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Models.Dto;
using Altinn.Studio.Designer.Services.Interfaces;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Authorization;
@@ -17,16 +18,18 @@
{
private readonly IGitea _giteaApi;
private readonly IAntiforgery _antiforgery;
private readonly IUserService _userService;

/// <summary>
/// Initializes a new instance of the <see cref="UserController"/> class.
/// </summary>
/// <param name="giteaWrapper">the gitea wrapper</param>
/// <param name="antiforgery">Access to the antiforgery system in .NET Core</param>
public UserController(IGitea giteaWrapper, IAntiforgery antiforgery)
public UserController(IGitea giteaWrapper, IAntiforgery antiforgery, IUserService userService)

Check warning on line 28 in backend/src/Designer/Controllers/UserController.cs

GitHub Actions / Run integration tests against actual gitea and db

Parameter 'userService' has no matching param tag in the XML comment for 'UserController.UserController(IGitea, IAntiforgery, IUserService)' (but other parameters do)

Check warning on line 28 in backend/src/Designer/Controllers/UserController.cs

GitHub Actions / Run dotnet build and test (macos-latest)

Parameter 'userService' has no matching param tag in the XML comment for 'UserController.UserController(IGitea, IAntiforgery, IUserService)' (but other parameters do)

Check warning on line 28 in backend/src/Designer/Controllers/UserController.cs

GitHub Actions / Run dotnet build and test (ubuntu-latest)

Parameter 'userService' has no matching param tag in the XML comment for 'UserController.UserController(IGitea, IAntiforgery, IUserService)' (but other parameters do)

Check warning on line 28 in backend/src/Designer/Controllers/UserController.cs

GitHub Actions / Analyze

Parameter 'userService' has no matching param tag in the XML comment for 'UserController.UserController(IGitea, IAntiforgery, IUserService)' (but other parameters do)

Check warning on line 28 in backend/src/Designer/Controllers/UserController.cs

GitHub Actions / Run dotnet build and test (windows-latest)

Parameter 'userService' has no matching param tag in the XML comment for 'UserController.UserController(IGitea, IAntiforgery, IUserService)' (but other parameters do)
{
_giteaApi = giteaWrapper;
_antiforgery = antiforgery;
_userService = userService;
}

/// <summary>
@@ -80,6 +83,14 @@
return success ? NoContent() : StatusCode(418);
}

[HttpGet]
[Route("org-permissions/{org}")]
public async Task<IActionResult> HasAccessToCreateRepository(string org)
{
UserRepositoryPermission userRepository = await _userService.GetUserRepositoryPermission(org);
return Ok(userRepository);
}

/// <summary>
/// Removes the star marking on the specified repository.
/// </summary>
1 change: 1 addition & 0 deletions backend/src/Designer/Infrastructure/ServiceRegistration.cs
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ public static IServiceCollection RegisterServiceImplementations(this IServiceCol
services.AddScoped<IReleaseRepository, ORMReleaseRepository>();
services.AddScoped<IDeploymentRepository, ORMDeploymentRepository>();
services.AddScoped<IAppScopesRepository, AppScopesRepository>();
services.AddScoped<IUserService, UserService>();
services.AddTransient<IReleaseService, ReleaseService>();
services.AddTransient<IDeploymentService, DeploymentService>();
services.AddTransient<IAppScopesService, AppScopesService>();
6 changes: 6 additions & 0 deletions backend/src/Designer/Models/Dto/UserRepositoryPermission.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Altinn.Studio.Designer.Models.Dto;

public class UserRepositoryPermission
{
public bool CanCreateOrgRepo { get; set; }
}
2 changes: 2 additions & 0 deletions backend/src/Designer/RepositoryClient/Model/Team.cs
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ public class Team
/// </summary>
public string Name { get; set; }

public bool can_create_org_repo { get; set; }

/// <summary>
/// The organization that owns the team
/// </summary>
44 changes: 44 additions & 0 deletions backend/src/Designer/Services/Implementation/UserService.cs
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<UserRepositoryPermission> GetUserRepositoryPermission(string org)
{
bool canCreateOrgRepo = await HasPermissionToCreateOrgRepo(org);
return new UserRepositoryPermission() { 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;

Check failure

Code scanning / CodeQL

Potentially dangerous use of non-short-circuit logic Error

Potentially dangerous use of non-short circuit logic.
}
}
9 changes: 9 additions & 0 deletions backend/src/Designer/Services/Interfaces/IUserService.cs
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<UserRepositoryPermission> GetUserRepositoryPermission(string org);
}
Loading