Files
Umbraco-CMS/src/Umbraco.Cms.Api.Management/Controllers/ManagementApiControllerBase.cs
Mole 21b0a7ffae New Backoffice: Fix feedback to users controller (#14031)
* Add specific not found results

* Add tests for the enable/disable not found tweak

* Cache ids and key in UserIdKeyResolver

* Don't cache null keys

* BackOffice not Backoffice

* Move fetching the user out of the ChangePasswordUsersController

* Move resolving user out of SetAvatar

* Move resolving user out of Update

* Return more specific notfound in bykey

* Use ErrorResult for all endpoints with unknown errors

* Split integration tests

* Add mappers

* Use ?: consistently

* Add reuseable iso code validator

* Validate ISO code

* Update supressions

* Use method from base to get current user key

* Rename ISo to Iso

* Use keys in services instead of user groups + Added a couple of new validations

---------

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2023-04-04 15:41:12 +02:00

47 lines
2.0 KiB
C#

using System.Linq.Expressions;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Common.Filters;
using Umbraco.Cms.Core.Security;
using Umbraco.New.Cms.Core;
namespace Umbraco.Cms.Api.Management.Controllers;
[JsonOptionsName(Constants.JsonOptionsNames.BackOffice)]
public class ManagementApiControllerBase : Controller
{
protected CreatedAtActionResult CreatedAtAction<T>(Expression<Func<T, string>> action, Guid id)
=> CreatedAtAction(action, new { id = id });
protected CreatedAtActionResult CreatedAtAction<T>(Expression<Func<T, string>> action, object routeValues)
{
if (action.Body is not ConstantExpression constantExpression)
{
throw new ArgumentException("Expression must be a constant expression.");
}
var controllerName = ManagementApiRegexes.ControllerTypeToNameRegex().Replace(typeof(T).Name, string.Empty);
var actionName = constantExpression.Value?.ToString() ?? throw new ArgumentException("Expression does not have a value.");
return base.CreatedAtAction(actionName, controllerName, routeValues, null);
}
protected CreatedAtActionResult CreatedAtAction<T>(Expression<Func<T, string>> action, string name)
{
if (action.Body is not ConstantExpression constantExpression)
{
throw new ArgumentException("Expression must be a constant expression.");
}
var controllerName = ManagementApiRegexes.ControllerTypeToNameRegex().Replace(typeof(T).Name, string.Empty);
var actionName = constantExpression.Value?.ToString() ?? throw new ArgumentException("Expression does not have a value.");
return base.CreatedAtAction(actionName, controllerName, new { name = name }, null);
}
protected static Guid CurrentUserKey(IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
{
//FIXME - Throw if no current user, when we are able to get the current user
return backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Key ?? Core.Constants.Security.SuperUserKey;
}
}