-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathUserController.cs
109 lines (100 loc) · 3.93 KB
/
UserController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System.Collections.Generic;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Helpers;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Models.Dto;
using Altinn.Studio.Designer.Services.Interfaces;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Altinn.Studio.Designer.Controllers
{
/// <summary>
/// API controller for User functionality
/// </summary>
[Authorize]
[Route("designer/api/user")]
public class UserController : ControllerBase
{
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, IUserService userService)
{
_giteaApi = giteaWrapper;
_antiforgery = antiforgery;
_userService = userService;
}
/// <summary>
/// Returns current logged in user
/// </summary>
/// <returns>The user object</returns>
[HttpGet]
[Route("current")]
public async Task<RepositoryClient.Model.User> Current()
{
// See comments in the configuration of Antiforgery in MvcConfiguration.cs.
var tokens = _antiforgery.GetAndStoreTokens(HttpContext);
HttpContext.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions
{
HttpOnly = false // Make this cookie readable by Javascript.
});
return await _giteaApi.GetCurrentUser();
}
/// <summary>
/// List the repos that the authenticated user owns or has access to
/// </summary>
/// <returns>List of repos</returns>
[HttpGet]
[Route("repos")]
public Task<IList<RepositoryClient.Model.Repository>> UserRepos()
{
return _giteaApi.GetUserRepos();
}
/// <summary>
/// Gets all starred repositories for the logged in user.
/// </summary>
/// <returns>An array of repositores that the user has starred</returns>
[HttpGet]
[Route("starred")]
public async Task<IList<RepositoryClient.Model.Repository>> GetStarredRepos()
{
return await _giteaApi.GetStarred();
}
/// <summary>
/// Adds the repository to the users list of starred repositories.
/// </summary>
[HttpPut]
[Route("starred/{org}/{repository}")]
public async Task<IActionResult> PutStarred(string org, string repository)
{
var success = await _giteaApi.PutStarred(org, repository);
return success ? NoContent() : StatusCode(418);
}
[HttpGet]
[Route("org-permissions/{org}")]
public async Task<IActionResult> HasAccessToCreateRepository(string org)
{
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
AltinnOrgContext editingContext = AltinnOrgContext.FromOrg(org, developer);
UserOrgPermission userOrg = await _userService.GetUserOrgPermission(editingContext);
return Ok(userOrg);
}
/// <summary>
/// Removes the star marking on the specified repository.
/// </summary>
[HttpDelete]
[Route("starred/{org}/{repository}")]
public async Task<IActionResult> DeleteStarred(string org, string repository)
{
var success = await _giteaApi.DeleteStarred(org, repository);
return success ? NoContent() : StatusCode(418);
}
}
}