/// <summary>
        /// Add a existing <c>Privilege</c> to a <c>Role</c>.
        /// </summary>
        /// <param name="roleId">Role Id</param>
        /// <param name="privilegeId">
        /// <c>Privilege Id</c>.
        /// If you don't know <c>Privilege Id</c>, you can call <see cref="PrivilegeHelper.GetId(string)"/> method.
        /// </param>
        /// <param name="depth"><see cref="PrivilegeDepth"/></param>
        /// <returns>
        /// <see cref="AddPrivilegesRoleResponse"/>
        /// </returns>
        public AddPrivilegesRoleResponse AddPrivilege(Guid roleId, Guid privilegeId, PrivilegeDepth depth)
        {
            ExceptionThrow.IfGuidEmpty(roleId, "roleId");
            ExceptionThrow.IfGuidEmpty(privilegeId, "privilegeId");

            RolePrivilege privilege = new RolePrivilege()
            {
                Depth = depth, PrivilegeId = privilegeId
            };

            return(AddPrivilege(roleId, privilege));
        }
Beispiel #2
0
        private string GetPrivilegeDepthLabel(PrivilegeDepth privilegeDepth)
        {
            switch (privilegeDepth)
            {
            case PrivilegeDepth.Basic:
                return("User");

            case PrivilegeDepth.Local:
                return("Division");

            case PrivilegeDepth.Deep:
                return("Division and sub-Divisions");

            case PrivilegeDepth.Global:
                return("Organization");

            default:
                throw new Exception("Not supported!");
            }
        }
        public static int SetPrivDepthMask(PrivilegeDepth privilege)
        {
            switch (privilege)
            {
            case PrivilegeDepth.Global:    //Global - Organization
                return(8);

            case PrivilegeDepth.Deep:    //Deep - Parent: Child Business Units
                return(4);

            case PrivilegeDepth.Local:    //Local - Business Unit
                return(2);

            case PrivilegeDepth.Basic:    //Basic - User
                return(1);

            default:
                return(0);
            }
        }
        public static string GetPrivilegeDepthMaskName(PrivilegeDepth depth)
        {
            switch (depth)
            {
            case PrivilegeDepth.Basic:
                return("Basic (User)");

            case PrivilegeDepth.Local:
                return("Local (Business Unit)");

            case PrivilegeDepth.Deep:
                return("Deep (Parent: Child Business Units)");

            case PrivilegeDepth.Global:
                return("Global (Organization)");

            default:
                return(string.Empty);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Add a privilege to the current role.
        /// </summary>
        /// <param name="Service">Organization Service</param>
        /// <param name="EntityLogicalName">Entity Logical Name</param>
        /// <param name="Type">Privilege Type</param>
        /// <param name="Depth">Privilege Level</param>
        public void AddPrivilege(IOrganizationService Service, string EntityLogicalName, PrivilegeType Type, PrivilegeDepth Depth)
        {
            RetrieveEntityRequest RERequest = new RetrieveEntityRequest()
            {
                LogicalName = base.LogicalName, EntityFilters = Microsoft.Xrm.Sdk.Metadata.EntityFilters.Privileges
            };
            RetrieveEntityResponse REResponse = Service.Execute(RERequest) as RetrieveEntityResponse;

            SecurityPrivilegeMetadata Privilege = (from Priv in REResponse.EntityMetadata.Privileges where Priv.PrivilegeType == Type select Priv).FirstOrDefault();

            if (Privilege != null)
            {
                AddPrivilegesRoleRequest Request = new AddPrivilegesRoleRequest()
                {
                    RoleId     = this.Id,
                    Privileges = new RolePrivilege[] {
                        new RolePrivilege {
                            BusinessUnitId = this.GetAttributeValue <Guid>("businessunitid"), PrivilegeId = Privilege.PrivilegeId, Depth = Depth
                        }
                    }
                };
                Service.Execute(Request);
            }
        }
Beispiel #6
0
 private string GetPrivilegeDepthLabel(PrivilegeDepth privilegeDepth)
 {
     switch (privilegeDepth)
     {
         case PrivilegeDepth.Basic:
             return "User";
         case PrivilegeDepth.Local:
             return "Division";
         case PrivilegeDepth.Deep:
             return "Division and sub-Divisions";
         case PrivilegeDepth.Global:
             return "Organization";
         default:
             throw new Exception("Not supported!");
     }
 }
        /// <summary>
        /// Add a existing <c>Privilege</c> with name (<c>prv</c> prefix) to a <c>Role</c>.
        /// </summary>
        /// <param name="roleName">Role name</param>
        /// <param name="privilegeId">
        /// <c>Privilege Id</c>.
        /// If you don't know <c>Privilege Id</c>, you can call <see cref="PrivilegeHelper.GetId(string)"/> method.
        /// </param>
        /// <param name="depth"><see cref="PrivilegeDepth"/></param>
        /// <returns><see cref="AddPrivilegesRoleResponse"/></returns>
        public AddPrivilegesRoleResponse AddPrivilege(string roleName, Guid privilegeId, PrivilegeDepth depth)
        {
            ExceptionThrow.IfGuidEmpty(privilegeId, "privilegeId");
            ExceptionThrow.IfNullOrEmpty(roleName, "roleName");

            Guid id = GetId(roleName);

            ExceptionThrow.IfGuidEmpty(id, "id", string.Format("'{0}' not found", roleName));

            RolePrivilege privilege = new RolePrivilege()
            {
                Depth = depth, PrivilegeId = privilegeId
            };

            return(AddPrivilege(id, privilege));
        }
        /// <summary>
        /// Add a existing <c>Privilege</c> with name (<c>prv</c> prefix) to a <c>Role</c>.
        /// </summary>
        /// <param name="roleName">Role name</param>
        /// <param name="privilegeName">Privilege name with <c>prv</c> prefix</param>
        /// <param name="depth"><see cref="PrivilegeDepth"/></param>
        /// <returns><see cref="AddPrivilegesRoleResponse"/></returns>
        public AddPrivilegesRoleResponse AddPrivilege(string roleName, string privilegeName, PrivilegeDepth depth)
        {
            ExceptionThrow.IfNullOrEmpty(privilegeName, "privilegeName");
            ExceptionThrow.IfNullOrEmpty(roleName, "roleName");

            Guid id = GetId(roleName);

            ExceptionThrow.IfGuidEmpty(id, "id", string.Format("'{0}' not found", roleName));

            PrivilegeHelper privilegeHelper = new PrivilegeHelper(this.OrganizationService);
            var             privilegeList   = privilegeHelper.GetPredefinedPrivileges();

            ExceptionThrow.IfNull(privilegeList, "privilegeList", "System pre-defined privileges not found (001)");
            ExceptionThrow.IfNullOrEmpty(privilegeList.Entities, "privilegeList.Entities", "System pre-defined privileges not found (002)");

            var existingRole = privilegeList.Entities.Where(d => d["name"].Equals(privilegeName)).SingleOrDefault();

            ExceptionThrow.IfNull(existingRole, "Privilege", string.Format("'{0}' privilege not found", privilegeName));

            RolePrivilege privilege = new RolePrivilege()
            {
                Depth = depth, PrivilegeId = existingRole.Id
            };

            return(AddPrivilege(id, privilege));
        }
 public RolePrivilege(PrivilegeDepth depth, Guid privilegeId, Guid businessId)
 {
     this.Depth          = depth;
     this.PrivilegeId    = privilegeId;
     this.BusinessUnitId = businessId;
 }