/// <summary>
        /// Updates a role.
        /// </summary>
        /// <param name="roleId">The id of the role to be updated.</param>
        /// <param name="roleInfo">The role containing the updated information.</param>
        /// <returns>Returns the updated role.</returns>
        public SecurityRoleInfo UpdateRole(string roleId, SecurityRoleInfo roleInfo)
        {
            Guid id = Guid.Empty;

            if (!Guid.TryParse(roleId, out id))
            {
                throw new ArgumentException($"{nameof(roleId)} must be a valid GUID");
            }

            if (id != roleInfo.Id)
            {
                throw new ArgumentException($"Unable to update role using id: {id}, and id: {roleInfo.Id}");
            }

            var roleRepository = ApplicationContext.Current.GetService <ISecurityRepositoryService>();

            if (roleRepository == null)
            {
                throw new InvalidOperationException($"{nameof(ISecurityRepositoryService)} not found");
            }

            roleInfo.Role?.Policies?.AddRange(roleInfo.Policies.Select(r => new SecurityPolicyInstance(r.Policy, r.Grant)));

            var updatedRole = roleRepository.SaveRole(roleInfo.Role);

            return(new SecurityRoleInfo(updatedRole));
        }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EditRoleModel"/> class.
 /// </summary>
 /// <param name="securityRoleInfo">The security role information.</param>
 public EditRoleModel(SecurityRoleInfo securityRoleInfo) : this()
 {
     this.Id           = securityRoleInfo.Role.Key.Value;
     this.IsObsolete   = securityRoleInfo.Role.ObsoletionTime != null;
     this.Description  = securityRoleInfo.Role.Description;
     this.Name         = securityRoleInfo.Name;
     this.RolePolicies = securityRoleInfo.Policies.Select(p => new PolicyViewModel(p)).OrderBy(q => q.Name).ToList();
     this.Policies     = this.RolePolicies.Select(p => p.Id.ToString()).ToList();
 }
        public override object Update(object data)
        {
            if (data is SecurityRole)
            {
                data = new SecurityRoleInfo(data as SecurityRole);
            }
            var td = data as SecurityRoleInfo;

            var retVal = base.Update(data) as SecurityRoleInfo;

            return(new SecurityRoleInfo(td.Entity));
        }
        /// <summary>
        /// Converts an <see cref="EditRoleModel"/> instance to a <see cref="SecurityRoleInfo"/> instance.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="roleInfo">The role information.</param>
        /// <returns>Returns the converted security role info instance.</returns>
        protected SecurityRoleInfo ToSecurityRoleInfo(EditRoleModel model, SecurityRoleInfo roleInfo)
        {
            roleInfo.Role.Description = model.Description;
            roleInfo.Role.Name        = model.Name;

            var addPoliciesList = this.GetNewPolicies(model.Policies.Select(Guid.Parse));

            roleInfo.Policies = addPoliciesList.Select(p => new SecurityPolicyInfo(p)
            {
                Grant = PolicyGrantType.Grant
            }).ToList();

            return(roleInfo);
        }
        public override object Create(object data, bool updateIfExists)
        {
            if (data is SecurityRole)
            {
                data = new SecurityRoleInfo(data as SecurityRole);
            }


            var retVal = base.Create(data, updateIfExists) as SecurityRoleInfo;
            var td     = data as SecurityRoleInfo;


            return(new SecurityRoleInfo(retVal.Entity));
        }
Beispiel #6
0
        public ActionResult Edit(EditRoleModel model)
        {
            SecurityRoleInfo roleInfo = null;

            if (ModelState.IsValid)
            {
                try
                {
                    roleInfo = this.AmiClient.GetRole(model.Id.ToString());

                    if (roleInfo == null)
                    {
                        TempData["error"] = Locale.RoleNotFound;

                        return(RedirectToAction("Index"));
                    }

                    this.AmiClient.UpdateRole(roleInfo.Id.ToString(), this.ToSecurityRoleInfo(model, roleInfo));

                    TempData["success"] = Locale.RoleUpdatedSuccessfully;

                    return(RedirectToAction("ViewRole", new { id = roleInfo.Id.ToString() }));
                }
                catch (Exception e)
                {
                    Trace.TraceError($"Unable to update role: {e}");
                }
            }

            if (roleInfo != null)
            {
                model.RolePolicies = roleInfo.Policies.Select(p => new PolicyViewModel(p)).OrderBy(q => q.Name).ToList();
            }

            model.PoliciesList = new List <SelectListItem>();
            model.PoliciesList.AddRange(this.GetAllPolicies().ToSelectList("Name", "Id", null, true));
            model.Policies = model.RolePolicies?.Select(p => p.Id.ToString()).ToList();

            TempData["error"] = Locale.UnableToUpdateRole;

            return(View(model));
        }
        /// <summary>
        /// Creates a security role.
        /// </summary>
        /// <param name="role">The security role to be created.</param>
        /// <returns>Returns the newly created security role.</returns>
        public SecurityRoleInfo CreateRole(SecurityRoleInfo role)
        {
            var roleRepository = ApplicationContext.Current.GetService <ISecurityRepositoryService>();

            var roleToCreate = new SecurityRole
            {
                Name        = role.Name,
                Description = role.Role.Description
            };

            if (role.Policies != null)
            {
                roleToCreate.Policies.AddRange(role.Policies.Select(p => new SecurityPolicyInstance(p.Policy, p.Grant)));
            }
            else
            {
                roleToCreate.Policies.AddRange(role.Role.Policies.Select(p => new SecurityPolicyInstance(p.Policy, p.GrantType)));
            }

            return(new SecurityRoleInfo(roleRepository.CreateRole(roleToCreate)));
        }
 /// <summary>
 /// Updates a role.
 /// </summary>
 /// <param name="roleId">The id of the role to be updated.</param>
 /// <param name="roleInfo">The role containing the updated information.</param>
 /// <returns>Returns the updated role.</returns>
 public SecurityRoleInfo UpdateRole(Guid roleId, SecurityRoleInfo roleInfo)
 {
     return(this.Client.Put <SecurityRoleInfo, SecurityRoleInfo>($"SecurityRole/{roleId}", roleInfo));
 }
 /// <summary>
 /// Creates a role in the IMS.
 /// </summary>
 /// <param name="role">The role to be created.</param>
 /// <returns>Returns the newly created role.</returns>
 public SecurityRoleInfo CreateRole(SecurityRoleInfo role)
 {
     return(this.Client.Post <SecurityRoleInfo, SecurityRoleInfo>("SecurityRole", role));
 }
Beispiel #10
0
 /// <summary>
 /// Updates a role.
 /// </summary>
 /// <param name="roleId">The id of the role to be updated.</param>
 /// <param name="roleInfo">The role containing the updated information.</param>
 /// <returns>Returns the updated role.</returns>
 public SecurityRoleInfo UpdateRole(string roleId, SecurityRoleInfo roleInfo)
 {
     return(this.Client.Put <SecurityRoleInfo, SecurityRoleInfo>($"role/{roleId}", this.Client.Accept, roleInfo));
 }
Beispiel #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RoleViewModel"/> class
 /// with a specific <see cref="SecurityRoleInfo"/> instance.
 /// </summary>
 /// <param name="securityRoleInfo">The <see cref="SecurityRoleInfo"/> instance.</param>
 public RoleViewModel(SecurityRoleInfo securityRoleInfo) : base(securityRoleInfo)
 {
     this.Description = securityRoleInfo.Role.Description;
     this.Name        = securityRoleInfo.Name;
     //HasPolicies = securityRoleInfo.Policies?.Any() == true;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SecurityViewModel"/> class
 /// with a specific <see cref="SecurityRoleInfo"/> instance.
 /// </summary>
 /// <param name="securityRoleInfo">The <see cref="SecurityRoleInfo"/> instance.</param>
 protected SecurityViewModel(SecurityRoleInfo securityRoleInfo) : this(securityRoleInfo.Role, securityRoleInfo.Policies)
 {
 }