Beispiel #1
0
        public void AddSystemSecurableObject(SecurableApplication application)
        {
            #region Adding the application initialization data.

            GatekeeperFactory.ApplicationSvc.Add(application);

            // adding the system securable object type.
            SecurableObjectType systemObjectType = new SecurableObjectType()
            {
                    Id = 0,
                    Application = application,
                    Name = "System",
                    Description = "System Securable Object Type"
            };

            // adding the systemObjectType as a securable object type.
            GatekeeperFactory.SecurableObjectTypeSvc.Add(systemObjectType);

            application.SecurableObjectType = systemObjectType;

            // adding the application as a securable object.
            this.AddSecurableObject(application);

            // defining the system administrator role.
            Role systemAdministerRole = new Role()
            {
                Application = application,
                Name = "Administrator",
                Description = "Administers the System",
                SecurableObjectType = systemObjectType
            };

            // defining the system user role.
            Role systemUserRole = new Role()
            {
                Application = application,
                Name = "User",
                Description = "Uses the System",
                SecurableObjectType = systemObjectType
            };

            // adding the system administrator and the system user roles.
            IRoleSvc roleSvc = GatekeeperFactory.RoleSvc;
            roleSvc.Add(systemAdministerRole);//adding the systemAdministerRole as a role.
            roleSvc.Add(systemUserRole);//adding the systemUserRole as a role.

            // defining the Administer_System right.
            Right administerSystemRight = new Right()
            {
                Application = application,
                Name = "Administer_System",
                Description = "Administers the System",
                SecurableObjectType = systemObjectType
            };

            // defining the View_System right.
            Right viewSystemRight = new Right()
            {
                Application = application,
                Name = "View_System",
                Description = "Views the System",
                SecurableObjectType = systemObjectType
            };

            // adding the Administer_System and the View_System rights.
            IRightSvc rightSvc = GatekeeperFactory.RightSvc;
            rightSvc.Add(administerSystemRight);//adding the administerSystemRight as a right.
            rightSvc.Add(viewSystemRight);//adding the viewSystemRight as a right.

            // adding the role-right assignment (System Admin - Administer_System)
            RoleRightAssignment admin_administer = new RoleRightAssignment()
            {
                Application = application,
                Role = systemAdministerRole,
                Right = administerSystemRight,
                SecurableObjectType = systemObjectType
            };

            // adding the role-right assignment (System Admin - View_System)
            RoleRightAssignment admin_view = new RoleRightAssignment()
            {
                Application = application,
                Role = systemAdministerRole,
                Right = viewSystemRight,
                SecurableObjectType = systemObjectType
            };

            // adding the role-right assignment (System User - View_System)
            RoleRightAssignment user_view = new RoleRightAssignment()
            {
                Application = application,
                Role = systemUserRole,
                Right = viewSystemRight,
                SecurableObjectType = systemObjectType
            };

            IRoleRightAssignmentSvc rraSvc = GatekeeperFactory.RoleRightAssignmentSvc;
            rraSvc.Add(admin_administer);
            rraSvc.Add(admin_view);
            rraSvc.Add(user_view);

            #endregion
        }
        /// <summary>
        /// Saves the associate rights to role.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <param name="rightIds">The right ids.</param>
        public void SaveAssociateRightsToRole([DataBind("role")]Role role, [DataBind("right.IsGranted")]int[] rightIds)
        {
            #region Logging
            if (log.IsDebugEnabled) log.Debug(Messages.MethodEnter);
            #endregion

            IRoleRightAssignmentSvc roleRightAssignmentSvc = GatekeeperFactory.RoleRightAssignmentSvc;

            //Gets role of a specified roleId.
            role = GatekeeperFactory.RoleSvc.Get(role.Id);

            //Gets application of a specified role.
            Application application = GatekeeperFactory.ApplicationSvc.Get(role.Application.Id);

            //Gets RoleRightAssignmentCollection of a specified role.
            RoleRightAssignmentCollection existingRoleRightAssignments = roleRightAssignmentSvc.Get(role);

            RoleRightAssignmentCollection selectedRoleRightAssignments = new RoleRightAssignmentCollection();

            foreach (int rightId in rightIds)
            {
                RoleRightAssignment rra = new RoleRightAssignment()
                {
                    Right = new Right() { Id = rightId },
                    Role = role,
                    Application = application,
                    SecurableObjectType = role.SecurableObjectType
                };

                selectedRoleRightAssignments.Add(rra);
            }

            RoleRightAssignmentCollection newRoleRightAssignments = new RoleRightAssignmentCollection();
            RoleRightAssignmentCollection deletedRoleRightAssignments = new RoleRightAssignmentCollection();

            foreach(RoleRightAssignment existingRra in existingRoleRightAssignments)
            {
                if (!selectedRoleRightAssignments.Contains(existingRra))
                    deletedRoleRightAssignments.Add(existingRra);
            }

            foreach (RoleRightAssignment selectedRra in selectedRoleRightAssignments)
            {
                if (!existingRoleRightAssignments.Contains(selectedRra))
                    newRoleRightAssignments.Add(selectedRra);
            }

            //Adds newRoleRightAssignments into the system.
            roleRightAssignmentSvc.Add(newRoleRightAssignments);

            //Deletes roleRightAssignment from the system.
            roleRightAssignmentSvc.Delete(deletedRoleRightAssignments);

            Hashtable args = new Hashtable();
            args["roleId"] = role.Id;

            this.RedirectToAction("displayRightsByRole", args);

            #region Logging
            if (log.IsDebugEnabled) log.Debug(Messages.MethodLeave);
            #endregion
        }