XAF Sample: Security Preview Controller

In this sample module, we allow certain users to switch between security roles quickly rather than having to log in and out as different users. This is ideal when using a single role per user but can easily be expanded to allow multiple roles.

NOTE: In this sample video and project, we replace the SecuredObjectSpaceProvider with a regular XPObjectSpaceProvider. In your solutions, you can keep the SecuredObjectSpaceProvider by using the following:

    public partial class SecurityPreviewController : WindowController
    {
        public SecurityPreviewController()
        {
            InitializeComponent();
            // Target required Windows (via the TargetXXX properties) and create their Actions.
            TargetWindowType = WindowType.Main;
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            // Perform various tasks depending on the target Window.
            LoadRoles();
        }

	private IObjectSpace CreateObjectSpace()
        {
            SecuredObjectSpaceProvider provider = (SecuredObjectSpaceProvider)Application.ObjectSpaceProviders.Where(x => x is SecuredObjectSpaceProvider).FirstOrDefault();
            return provider == null ? Application.CreateObjectSpace() : provider.CreateNonsecuredObjectSpace();
        }
		
        private void LoadRoles()
        {
            actionSecurityPreview.Items.Clear();

            using (IObjectSpace space = CreateObjectSpace())
            {
                ISecurityPreview user = (ISecurityPreview)SecuritySystem.CurrentUser;

                if (user.SecurityPreview)
                {
                    foreach (PermissionPolicyRole role in space.GetObjects().OrderBy(x => x.Name))
                    {
                        ChoiceActionItem item = new ChoiceActionItem(role.Name, role.Oid);
                        actionSecurityPreview.Items.Add(item);
                    }

                    actionSecurityPreview.Items.Add(new ChoiceActionItem("Refresh...", Guid.Empty));
                }
            }
        }

        private void actionSecurityPreview_Execute(object sender, SingleChoiceActionExecuteEventArgs e)
        {
            Guid roleoid = (Guid)e.SelectedChoiceActionItem.Data;

            if (roleoid.Equals(Guid.Empty))
            {
                LoadRoles();
                return;
            }

            using (IObjectSpace space = CreateObjectSpace())
            {
                PermissionPolicyRole role = space.GetObjectByKey(roleoid);
                PermissionPolicyUser user = space.GetObjectByKey(SecuritySystem.CurrentUserId);

                if (user == null || role == null)
                    throw new UserFriendlyException("Unable to find user or role");

                while (user.Roles.Count > 0)
                    user.Roles.Remove(user.Roles[0]);

                user.Roles.Add(role);

                space.CommitChanges();
                Application.LogOff();
            }
        }
    }

 

Download the sample project below…