Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Roger 14 posts 34 karma points
    Jul 07, 2017 @ 15:00
    Roger
    0

    Admin Only true/false

    I am using a true/false datatype on a topic page that I check the value to enable or disable a postback method in a controller. Everything works fine, but I need to limit who can can change the value. I just want the surper admin(0) to be able to change its value

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Jul 07, 2017 @ 15:15
    Nicholas Westby
    0

    I made a property editor that is a text field that can only be edited by users belonging to the user types you select in the data type configuration screen (everybody else gets a read only version). Would be very easy to modify to make it work with a checkbox instead. Here's the code for each file.

    UserTypeApiController.cs

    namespace MyProject.App.Controllers
    {
    
        // Namespaces.
        using System.Linq;
        using System.Web.Http;
        using Umbraco.Web.Mvc;
        using Umbraco.Web.WebApi;
    
        /// <summary>
        /// Controller for Umbraco user types.
        /// </summary>
        [PluginController("Rhythm")]
        public class UserTypeApiController : UmbracoAuthorizedApiController
        {
    
            #region Action Methods
    
            /// <summary>
            /// Returns the ID of the current user's user type.
            /// </summary>
            /// <returns>
            /// The CSV path.
            /// </returns>
            /// <remarks>
            /// The URL is "/umbraco/backoffice/Rhythm/UserTypeApi/CurrentUserTypeId".
            /// </remarks>
            [HttpGet]
            public int CurrentUserTypeId()
            {
                return this.UmbracoContext.Security.CurrentUser.UserType.Id;
            }
    
            /// <summary>
            /// Returns all user types in Umbraco.
            /// </summary>
            /// <returns>
            /// The user types (an array of objects, each containing an ID and name).
            /// </returns>
            /// <remarks>
            /// The URL is "/umbraco/backoffice/Rhythm/UserTypeApi/GetUserTypes".
            /// </remarks>
            [HttpGet]
            public object GetUserTypes()
            {
                return this.Services.UserService.GetAllUserTypes()
                    .OrderBy(x => x.Name).Select(x => new
                    {
                        id = x.Id,
                        name = x.Name
                    }).ToArray();
            }
    
            #endregion
    
        }
    
    }
    

    App_Plugins/ProtectedText/user-type-picker/user-type-picker.html

    <user-type-picker model="model"></user-type-picker>
    

    App_Plugins/ProtectedText/user-type-picker/user-type-picker.js

    (function () {
    
        /**
         * The controller for the user type picker directive.
         */
        function Controller($scope, $http) {
            this.injected = {
                $scope: $scope,
                $http: $http
            };
            this.getUserTypes();
            $scope.isSelected = this.isSelected.bind(this);
        }
    
        /**
         * Gets all of the user types and stores them on the scope.
         */
        Controller.prototype.getUserTypes = function () {
            var url = "/umbraco/backoffice/Rhythm/UserTypeApi/GetUserTypes";
            var $scope = this.injected.$scope;
            var $http = this.injected.$http;
            $http.get(url).success(function (data) {
                $scope.userTypes = data.map(function (item) {
                    return {
                        id: item.id.toString(),
                        name: item.name
                    };
                });
            });
        };
    
        /**
         * Indicates whether or not the specified option is selected.
         * @param userType The user type to check for selection against.
         */
        Controller.prototype.isSelected = function (userType) {
            var $scope = this.injected.$scope;
            var items = $scope.model.value;
            if (items && items.length) {
                for (var i = 0; i < items.length; i++) {
                    if (items[i] === userType.id) {
                        return true;
                    }
                }
            }
            return false;
        };
    
        /**
         * A directive that allows user types to be picked.
         * @returns {{restrict: string, templateUrl: string, controller: Controller, scope: {model: string}}}
         */
        function directive() {
            return {
                restrict: "E",
                templateUrl: "/App_Plugins/ProtectedText/user-type-picker/user-type-picker-directive.html",
                controller: Controller,
                scope: {
                    model: "="
                }
            };
        }
    
        // Register directive.
        angular.module("umbraco").directive("userTypePicker", directive);
    
    })();
    

    App_Plugins/ProtectedText/user-type-picker/user-type-picker-directive.html

    <select multiple size="20" ng-model="model.value">
        <option ng-repeat="type in userTypes" value="{{type.id}}" ng-selected="isSelected(type)">{{type.name}}</option>
    </select>
    

    App_Plugins/ProtectedText/package.manifest

    {
      "propertyEditors": [{
        "alias": "Rhythm.ProtectedText",
        "name": "Protected Text",
        "icon": "icon-code",
        "group": "Text",
        "editor": {
          "view": "~/App_Plugins/ProtectedText/protected-text.html",
          "valueType": "TEXT"
        },
        "prevalues": {
          "fields": [
            {
              "label": "User Types",
              "description": "These are the user types who will be allowed to edit the field data.",
              "key": "userTypes",
              "view": "~/App_Plugins/ProtectedText/user-type-picker/user-type-picker.html"
            },
            {
              "label": "Multiline?",
              "description": "Use a multiline text area rather than a single line text field.",
              "key": "multiline",
              "view": "boolean"
            }
          ]
        }
      }],
      "javascript": [
        "~/App_Plugins/ProtectedText/protected-text.js",
        "~/App_Plugins/ProtectedText/user-type-picker/user-type-picker.js"
      ]
    }
    

    App_Plugins/ProtectedText/protected-text.html

    <protected-text model="model"></protected-text>
    

    App_Plugins/ProtectedText/protected-text-directive.html

    <div>
        <input ng-if="model.config.multiline === '0'" ng-readonly="readonly" type="text" ng-model="model.value" class="umb-editor umb-textstring" />
        <textarea ng-if="model.config.multiline === '1'" ng-readonly="readonly" type="text" ng-model="model.value" class="umb-editor umb-textarea" rows="10"></textarea>
    </div>
    

    App_Plugins/ProtectedText/protected-text.js

    (function () {
    
        /**
         * The controller for the protected text directive.
         */
        function Controller($scope, $http) {
            this.injected = {
                $scope: $scope,
                $http: $http
            };
            $scope.readonly = true;
            this.checkPermission();
        }
    
        /**
         * Enables or disables the text field based on whether or not the current user has permission
         * to edit the field.
         */
        Controller.prototype.checkPermission = function () {
            var url = "/umbraco/backoffice/Rhythm/UserTypeApi/CurrentUserTypeId";
            var $scope = this.injected.$scope;
            var $http = this.injected.$http;
            $http.get(url).success(function (data) {
                var currentUserId = JSON.parse(data).toString();
                var items = $scope.model.config.userTypes || [];
                for (var i = 0; i < items.length; i++) {
                    if (currentUserId === items[i]) {
                        $scope.readonly = false;
                        return;
                    }
                }
                $scope.readonly = true;
            });
        };
    
        /**
         * The directive that facilitates protecting text fields so only some user types can edit them.
         * @returns {{restrict: string, templateUrl: string, controller: Controller, scope: {model: string}}}
         */
        function directive() {
            return {
                restrict: "E",
                templateUrl: "/App_Plugins/ProtectedText/protected-text-directive.html",
                controller: Controller,
                scope: {
                    model: "="
                }
            };
        }
    
        // Register directive.
        angular.module("umbraco").directive("protectedText", directive);
    
    })();
    
  • Roger 14 posts 34 karma points
    Jul 07, 2017 @ 15:27
    Roger
    0

    Nicholas, Thank you for the Input, At first I was just looking for a way to validate the user for just the enable checkbox, the I looked at the who process of where I was storing the two other values I needed for my postback method. So now I am heading down the direction you suggested and I am going to change the datatype to have a true/false and two text boxes, one for the end point url and the other for the data string to pass.

Please Sign in or register to post replies

Write your reply to:

Draft