Exemplo n.º 1
0
        public IHttpActionResult addRole([FromBody] RoleBindingModels newRole)
        {
            try
            {
                ApplicationDbContext context = new ApplicationDbContext();

                var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));

                if (!roleManager.RoleExists(newRole.Name))
                {
                    var role = new IdentityRole(newRole.Name);
                    roleManager.Create(role);
                    log.Debug("addrole - ruolo + [" + newRole.Name + "] aggiunto con successo");
                    return(Ok(role));
                }
                else
                {
                    log.Error("addrole - ruolo + [" + newRole.Name + "] già esistente");
                    return(BadRequest("Role [" + newRole.Name + "] already profiled"));
                }
            }
            catch (Exception ex)
            {
                log.Error("addrole - errore nell'esecuzione");
                return(InternalServerError(ex));
            }
        }
Exemplo n.º 2
0
        // POST api/roles
        //[Route("create")]
        //[HttpPost]
        //public async Task<IHttpActionResult> Post(RoleBindingModels model)
        //{
        //    if (!ModelState.IsValid)
        //    {
        //        return BadRequest(ModelState);
        //    }

        //    var role = new IdentityRole { Name = model.Name };

        //    var result = await this.RoleManager.CreateAsync(role);

        //    if (!result.Succeeded)
        //    {
        //        return GetErrorResult(result);
        //    }

        //    Uri locationHeader = new Uri(Url.Link("GetRoleById", new { id = role.Id }));

        //    return Created(locationHeader, TheModelFactory.Create(role));

        //}

        //[Route("{id:guid}")]
        public async Task <IHttpActionResult> DeleteRole([FromBody] RoleBindingModels Id)
        {
            var role = await this.RoleManager.FindByIdAsync(Id.ToString());

            if (role != null)
            {
                IdentityResult result = await this.RoleManager.DeleteAsync(role);

                //if (!result.Succeeded)
                //{
                //    return GetErrorResult(result);
                //}

                return(Ok());
            }

            return(NotFound());
        }
Exemplo n.º 3
0
        public string ManageUsersInRole(RoleBindingModels.UsersInRoleModel model)
        {
            var role = AppRoleManager.FindById(model.Id);

            if (role == null)
            {
                ModelState.AddModelError("", "Role does not exist");
                return "BadRequest(ModelState)";
            }

            foreach (string user in model.EnrolledUsers)
            {
                var appUser = UserManager.FindById(user);

                if (appUser == null)
                {
                    ModelState.AddModelError("", String.Format("User: {0} does not exists", user));
                    continue;
                }

                if (!UserManager.IsInRole(user, role.Name))
                {
                    IdentityResult result = UserManager.AddToRole(user, role.Name);

                    if (!result.Succeeded)
                    {
                        ModelState.AddModelError("", String.Format("User: {0} could not be added to role", user));
                    }

                }
            }

            foreach (string user in model.RemovedUsers)
            {
                var appUser = UserManager.FindById(user);

                if (appUser == null)
                {
                    ModelState.AddModelError("", String.Format("User: {0} does not exists", user));
                    continue;
                }

                IdentityResult result = UserManager.RemoveFromRole(user, role.Name);

                if (!result.Succeeded)
                {
                    ModelState.AddModelError("", String.Format("User: {0} could not be removed from role", user));
                }
            }

            if (!ModelState.IsValid)
            {
                return "BadRequest(ModelState)";
            }

            return "Ok()";
        }
Exemplo n.º 4
0
        public string CreateRole(RoleBindingModels.CreateRoleBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return "BadRequest(ModelState)";
            }

            var role = new IdentityRole { Name = model.Name };

            var result = AppRoleManager.CreateAsync(role);

            if (!result.Result.Succeeded)
            {
                return "GetErrorResult(result)";
            }

            //Uri locationHeader = new Uri(Url.Link("GetRoleById", new { id = role.Id }));

            return "Created(locationHeader, TheModelFactory.Create(role))";

        }