<commit_before>﻿namespace UserGroupPermissions.Controllers
{

    // Namespaces.
    using Businesslogic;
    using Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http;
    using umbraco.interfaces;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Models.Membership;
    using Umbraco.Web;
    using Umbraco.Web.Editors;
    using Umbraco.Web.Mvc;
    using Umbraco.Web.WebApi.Filters;
    using Constants = Umbraco.Core.Constants;


    /// <summary>
    /// Controller for user group permission operations that occur on the server.
    /// </summary>
    [PluginController("UGP")]
    [UmbracoApplicationAuthorize(Constants.Applications.Users)]
    public class UserGroupPermissionsController : UmbracoAuthorizedJsonController
    {

        #region Constants

        private const string UserNotFound = "The specified user was not found.";

        #endregion


        #region Readonly Variables

        private readonly UserTypePermissionsService _userTypePermissionsService;

        #endregion


        #region Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        public UserGroupPermissionsController() : this(UmbracoContext.Current)
        {
        }


        /// <summary>
        /// Primary constructor.
        /// </summary>
        public UserGroupPermissionsController(UmbracoContext umbracoContext)
            : base(umbracoContext)
        {
            _userTypePermissionsService = new UserTypePermissionsService();
        }

        #endregion


        #region Web Methods

        /// <summary>
        /// Applies all user group permissions for the specified user.
        /// </summary>
        /// <param name="request">
        /// The request parameters.
        /// </param>
        /// <returns>
        /// An object indicating the success (or failure) of the operation.
        /// </returns>
        [HttpPost]
        public object ApplyAllGroupPermissions(ApplyRequest request)
        {

            // Variables.
            var failureReason = default(string);
            var userService = Services.UserService;
            var user = userService.GetUserById(request.UserId);
            var success = true;


            // User found?
            if (user == null)
            {
                success = false;
                failureReason = UserNotFound;
            }


            // Copy permissions.
            if (success)
            {
                _userTypePermissionsService.CopyPermissionsForSingleUser(user);
            }


            // Indicate success or failure.
            if (success)
            {
                return new
                {
                    Success = true
                };
            }
            else
            {
                return new
                {
                    Success = false,
                    Reason = failureReason
                };
            }

        }


        /// <summary>
        /// Applies the user group permissions to the specified node.
        /// </summary>
        /// <param name="request">
        /// The request parameters.
        /// </param>
        /// <returns>
        /// An object indicating the success (or failure) of the operation.
        /// </returns>
        [HttpPost]
        public object SetGroupPermissions(SetPermissionsRequest request)
        {

            // Variables.
            var userService = Services.UserService;
            var contentService = ApplicationContext.Services.ContentService;
            var userPermissions = request.UserTypePermissions;
            var nodeId = request.NodeId;
            var ignoreBase = request.IgnoreBasePermissions;
            var node = contentService.GetById(nodeId);
            var permissionsByTypeId = new Dictionary<int, string[]>();


            // Add all user types to dictionary.
            foreach (var u in userService.GetAllUserTypes())
            {
                //check if any permissions were set
                var newUserTypePermissions = userPermissions.FirstOrDefault(x => x.UserTypeId == u.Id);
                if (newUserTypePermissions == null) continue;

                //grab new permissions
                var permissions = newUserTypePermissions.Permissions.ToList();
                if (!permissions.Any() && !(ignoreBase && !u.Permissions.Any()))
                {
                    //no permissions set means disable all permissions
                    permissions.Add("-");
                }

                //if we are ignoring base then remove set base
                if (ignoreBase)
                {
                    permissions = permissions.Where(x => !u.Permissions.Contains(x)).ToList();
                }


                //if we have any permissions set
                permissionsByTypeId[u.Id] = permissions.ToArray();

            }


            // Process each user type with permissions.
            foreach (var pair in permissionsByTypeId)
            {

                // Variables.
                var userType = userService.GetUserTypeById(pair.Key);


                // Update user type permissions.
                _userTypePermissionsService.UpdateCruds(userType, node, pair.Value.Select(x=>x[0]));


                // Update user permissions?
                if (request.ReplacePermissionsOnUsers)
                {
                    _userTypePermissionsService.CopyPermissions(userType, node);
                }

            }


            // Indicate success.
            return new
            {
                Success = true
            };

        }


        /// <summary><commit_msg>option to exclude base permissions<commit_after>﻿namespace UserGroupPermissions.Controllers
{

    // Namespaces.
    using Businesslogic;
    using Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http;
    using umbraco.interfaces;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Models.Membership;
    using Umbraco.Web;
    using Umbraco.Web.Editors;
    using Umbraco.Web.Mvc;
    using Umbraco.Web.WebApi.Filters;
    using Constants = Umbraco.Core.Constants;


    /// <summary>
    /// Controller for user group permission operations that occur on the server.
    /// </summary>
    [PluginController("UGP")]
    [UmbracoApplicationAuthorize(Constants.Applications.Users)]
    public class UserGroupPermissionsController : UmbracoAuthorizedJsonController
    {

        #region Constants

        private const string UserNotFound = "The specified user was not found.";

        #endregion


        #region Readonly Variables

        private readonly UserTypePermissionsService _userTypePermissionsService;

        #endregion


        #region Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        public UserGroupPermissionsController() : this(UmbracoContext.Current)
        {
        }


        /// <summary>
        /// Primary constructor.
        /// </summary>
        public UserGroupPermissionsController(UmbracoContext umbracoContext)
            : base(umbracoContext)
        {
            _userTypePermissionsService = new UserTypePermissionsService();
        }

        #endregion


        #region Web Methods

        /// <summary>
        /// Applies all user group permissions for the specified user.
        /// </summary>
        /// <param name="request">
        /// The request parameters.
        /// </param>
        /// <returns>
        /// An object indicating the success (or failure) of the operation.
        /// </returns>
        [HttpPost]
        public object ApplyAllGroupPermissions(ApplyRequest request)
        {

            // Variables.
            var failureReason = default(string);
            var userService = Services.UserService;
            var user = userService.GetUserById(request.UserId);
            var success = true;


            // User found?
            if (user == null)
            {
                success = false;
                failureReason = UserNotFound;
            }


            // Copy permissions.
            if (success)
            {
                _userTypePermissionsService.CopyPermissionsForSingleUser(user);
            }


            // Indicate success or failure.
            if (success)
            {
                return new
                {
                    Success = true
                };
            }
            else
            {
                return new
                {
                    Success = false,
                    Reason = failureReason
                };
            }

        }


        /// <summary>
        /// Applies the user group permissions to the specified node.
        /// </summary>
        /// <param name="request">
        /// The request parameters.
        /// </param>
        /// <returns>
        /// An object indicating the success (or failure) of the operation.
        /// </returns>
        [HttpPost]
        public object SetGroupPermissions(SetPermissionsRequest request)
        {

            // Variables.
            var userService = Services.UserService;
            var contentService = ApplicationContext.Services.ContentService;
            var userPermissions = request.UserTypePermissions;
            var nodeId = request.NodeId;
            var ignoreBase = request.IgnoreBasePermissions;
            var node = contentService.GetById(nodeId);
            var permissionsByTypeId = new Dictionary<int, string[]>();
            var assignablePermissions = ActionsResolver.Current.Actions.Where(x => x.CanBePermissionAssigned).Select(x=>x.Letter);


            // Add all user types to dictionary.
            foreach (var u in userService.GetAllUserTypes())
            {
                //check if any permissions were set
                var newUserTypePermissions = userPermissions.FirstOrDefault(x => x.UserTypeId == u.Id);
                if (newUserTypePermissions == null) continue;
                var existingPermissions = u.Permissions.Where(x => assignablePermissions.Contains(x[0])).ToList();

                //grab new permissions
                var permissions = newUserTypePermissions.Permissions.ToList();
                if (!permissions.Any() && !(ignoreBase && !existingPermissions.Any()))
                {
                    //no permissions set means disable all permissions
                    permissions.Add("-");
                }

                //if we ignore when set permissions match base permissions
                if (ignoreBase && permissions.All(x => existingPermissions.Contains(x)) && existingPermissions.All(x => permissions.Contains(x)))
                {
                    permissions = new List<string>();
                }


                //if we have any permissions set
                permissionsByTypeId[u.Id] = permissions.ToArray();

            }


            // Process each user type with permissions.
            foreach (var pair in permissionsByTypeId)
            {

                // Variables.
                var userType = userService.GetUserTypeById(pair.Key);


                // Update user type permissions.
                _userTypePermissionsService.UpdateCruds(userType, node, pair.Value.Select(x => x[0]));


                // Update user permissions?
                if (request.ReplacePermissionsOnUsers)
                {
                    _userTypePermissionsService.CopyPermissions(userType, node);
                }

            }


            // Indicate success.
            return new
            {
                Success = true
            };

        }


        /// <summary>
{'fraction_non_alphanumeric': 0.08780240230079513, 'fraction_numerical': 0.00025376416849940786, 'mean_word_length': 1.4990488268864934, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 54, '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': '1792443', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2703, 'n_words': 688}