<commit_before>﻿using System;
using Core.DomainModel;
using Core.DomainServices;
using Presentation.Web.Infrastructure.Attributes;
using System.Linq;
using System.Web.Http;
using Infrastructure.Services.Types;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;

namespace Presentation.Web.Controllers.OData
{
    [InternalApi]
    public class UsersController : BaseEntityController<User>
    {
        private readonly IUserService _userService;
        private readonly IGenericRepository<User> _repository;

        public UsersController(
            IGenericRepository<User> repository,
            IUserService userService)
            : base(repository)
        {
            _userService = userService;
            _repository = repository;
        }

        [NonAction]
        public override IHttpActionResult Post(int organizationId, User entity) => throw new NotSupportedException();

        protected override Maybe<IHttpActionResult> ValidatePatch(Delta<User> delta, User entity)
        {
            var error = base.ValidatePatch(delta, entity);
            if (error.HasValue)
                return error;
            if (AttemptToChangeStakeHolderAccess(delta, entity))
            {
                if (!UserContext.IsGlobalAdmin())
                {
                    error = Forbidden(); //Only global admins may edit stake holders
                }
            }

            return error;
        }

        private static bool AttemptToChangeStakeHolderAccess(Delta<User> delta, User entity)
        {
            return delta.TryGetPropertyValue(nameof(Core.DomainModel.User.HasStakeHolderAccess),
                out var hasStakeHolderAccess) && ((bool) hasStakeHolderAccess) != entity.HasStakeHolderAccess;
        }

        [HttpPost]
        public IHttpActionResult Create(ODataActionParameters parameters)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);<commit_msg>simplify flow<commit_after>﻿using System;
using Core.DomainModel;
using Core.DomainServices;
using Presentation.Web.Infrastructure.Attributes;
using System.Linq;
using System.Web.Http;
using Infrastructure.Services.Types;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;

namespace Presentation.Web.Controllers.OData
{
    [InternalApi]
    public class UsersController : BaseEntityController<User>
    {
        private readonly IUserService _userService;
        private readonly IGenericRepository<User> _repository;

        public UsersController(
            IGenericRepository<User> repository,
            IUserService userService)
            : base(repository)
        {
            _userService = userService;
            _repository = repository;
        }

        [NonAction]
        public override IHttpActionResult Post(int organizationId, User entity) => throw new NotSupportedException();

        protected override Maybe<IHttpActionResult> ValidatePatch(Delta<User> delta, User entity)
        {
            var error = base.ValidatePatch(delta, entity);
            if (error.IsNone)
            {
                if (AttemptToChangeStakeHolderAccess(delta, entity))
                {
                    if (!UserContext.IsGlobalAdmin())
                    {
                        error = Forbidden(); //Only global admins may edit stake holders
                    }
                }
            }

            return error;
        }

        private static bool AttemptToChangeStakeHolderAccess(Delta<User> delta, User entity)
        {
            return delta.TryGetPropertyValue(nameof(Core.DomainModel.User.HasStakeHolderAccess),
                out var hasStakeHolderAccess) && ((bool) hasStakeHolderAccess) != entity.HasStakeHolderAccess;
        }

        [HttpPost]
        public IHttpActionResult Create(ODataActionParameters parameters)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
{'fraction_non_alphanumeric': 0.07350727546412443, 'fraction_numerical': 0.0, 'mean_word_length': 2.262684124386252, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7145733', 'n_tokens_mistral': 981, 'n_tokens_neox': 961, 'n_words': 218}