public async Task <IActionResult> DeleteRole([FromBody] DeleteRoleModel model)
        {
            var isAdmin = await Utilities.IsUserSuperAdmin(User, _userManager);

            if (!isAdmin)
            {
                return(Unauthorized());
            }

            var role = await _roleManager.FindByNameAsync(model.RoleName);

            if (role == null)
            {
                return(RequestHandler.BadRequest($"Role {model.RoleName} does not exists"));
            }

            var result = await _roleManager.DeleteAsync(role);

            if (!result.Succeeded)
            {
                return(RequestHandler.BadRequest(result.Errors));
            }

            return(Ok());
        }
    public async Task <IActionResult> DeleteRole(DeleteRoleModel model, IFormCollection collection)
    {
        if (model == null)
        {
            throw new ArgumentNullException(nameof(model));
        }

        if (ModelState.IsValid)
        {
            if (collection.Any(x => string.Equals(x.Key, "delete", StringComparison.OrdinalIgnoreCase)))
            {
                var r = await _roleMgr.FindByNameAsync(model.Role);

                model.Result = await _roleMgr.DeleteAsync(r);

                return(RedirectToAction(nameof(ManageRoles)));
            }
            else
            {
                return(RedirectToAction(nameof(ManageRoles)));
            }
        }
        else
        {
            model.Result = IdentityResult.Failed();
            LogValidationErrors();
        }

        return(View(model));
    }
Example #3
0
        public async Task DeleteRoleAsync(DeleteRoleModel model)
        {
            var result = await roleManager.FindByIdAsync(model.Id.ToString());

            if (result != null)
            {
                await roleManager.DeleteAsync(result);
            }
        }
Example #4
0
        public async Task <ActionResult> DeleteRole([Bind(Include = "name")] DeleteRoleModel model)
        {
            ActionResult result = null;

            await Roles.DeleteRole(model.Name);

            result = Redirect(Request.UrlReferrer.ToString());

            return(result);
        }
Example #5
0
        public ActionResult _Delete(int id, string No, string Name, string Description)
        {
            var model = new DeleteRoleModel
            {
                Id          = id,
                No          = No,
                Name        = Name,
                Description = Description
            };

            return(View(model));
        }
    public IActionResult DeleteRole(string id)
    {
        if (string.IsNullOrEmpty(id))
        {
            return(RedirectToAction(nameof(Index)));
        }

        var model = new DeleteRoleModel
        {
            Role = id
        };

        return(View(model));
    }
Example #7
0
        public async Task <IActionResult> DeleteRole(DeleteRoleModel model)
        {
            try
            {
                await administrativeService.DeleteRoleAsync(model);

                return(Ok());
            }
            catch (Exception ex)
            {
                this.log.Error(string.Format(Conts.Conts.ApiErrorMessageLog, ControllerContext.ActionDescriptor.ControllerName, ControllerContext.ActionDescriptor.ActionName), ex);
                return(BadRequest(new { message = Conts.Conts.ApiErrorMessageResponse }));
            }
        }
Example #8
0
        /// <summary>
        /// Delete Role.
        /// </summary>
        /// <param name="UserGuid">Role Identifier.</param>
        /// <returns></returns>
        public async Task <DataResponse <DeleteRoleResult> > DeleteRole(string roleGuid)
        {
            try
            {
                DeleteRoleModel request   = new DeleteRoleModel();
                var             portalApi = await _ioTConnectAPIDiscovery.GetPortalUrl(_envCode, _solutionKey, IoTConnectBaseURLType.UserBaseUrl);

                string accessTokenUrl = string.Concat(portalApi, UserApi.DeleteRole);
                string formattedUrl   = String.Format(accessTokenUrl, Constants.userVersion, roleGuid);
                var    deleteRole     = await formattedUrl.WithHeaders(new { Content_type = Constants.contentType, Authorization = Constants.bearerTokenType + _token })
                                        .SendJsonAsync(HttpMethod.Delete, request).ReceiveJson <DataResponse <List <DeleteRoleResult> > >();

                return(new DataResponse <DeleteRoleResult>(null)
                {
                    data = deleteRole.data.FirstOrDefault(),
                    message = deleteRole.message,
                    status = true
                });
            }
            catch (IoTConnectException ex)
            {
                List <ErrorItemModel> errorItemModels = new List <ErrorItemModel>();
                errorItemModels.AddRange(ex.error);
                return(new DataResponse <DeleteRoleResult>(null)
                {
                    errorMessages = errorItemModels,
                    message = ex.message,
                    status = false
                });
            }
            catch (Exception ex)
            {
                await _ioTConnectAPIDiscovery.LoggedException(_envCode, ex, "User", "DeleteRole()");

                throw ex;
            }
        }
Example #9
0
        public async Task Roles_Create_Delete_Role()
        {
            DeleteRoleModel modelDelete = new DeleteRoleModel()
            {
                DeleteRole = "TestRole"
            };
            CreateRoleModel model = new CreateRoleModel()
            {
                NewRole = "TestRole"
            };

            LoginTokenModel token = await getAdminToken();
            _client.DefaultRequestHeaders.Add("Authorization", String.Format("Bearer {0}", token.access_token));

            string str = String.Format("NewRole={0}", Uri.EscapeUriString(model.NewRole));
            StringContent theContent = new StringContent(str, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
            HttpResponseMessage response = await _client.PostAsync("api/Account/AddRole", theContent);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            str = String.Format("DeleteRole={0}", Uri.EscapeUriString(modelDelete.DeleteRole));
            theContent = new StringContent(str, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
            response = await _client.PostAsync("api/Account/DeleteRole", theContent);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            clearToken();
        }
Example #10
0
        public async Task Roles_Create_Delete_Add_Remove_Unauthorized()
        {
            CreateRoleModel modelCreate = new CreateRoleModel()
            {
                NewRole = "Admin"
            };

            string str = String.Format("NewRole={0}", Uri.EscapeUriString(modelCreate.NewRole));
            StringContent theContent = new StringContent(str, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
            HttpResponseMessage response = await _client.PostAsync("api/Account/AddRole", theContent);
            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);

            DeleteRoleModel modelDelete = new DeleteRoleModel()
            {
                DeleteRole = "TestRole"
            };

            str = String.Format("DeleteRole={0}", Uri.EscapeUriString(modelDelete.DeleteRole));
            theContent = new StringContent(str, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
            response = await _client.PostAsync("api/Account/DeleteRole", theContent);
            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);

            UserToRoleModel model = new UserToRoleModel()
            {
                Role = "Test",
                User = "******"
            };

            str = String.Format("Role={0}&User={1}", Uri.EscapeUriString(model.Role), Uri.EscapeUriString(model.User));
            theContent = new StringContent(str, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
            response = await _client.PostAsync("api/Account/AddUserToRole", theContent);
            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);

            str = String.Format("Role={0}&User={1}", Uri.EscapeUriString(model.Role), Uri.EscapeUriString(model.User));
            theContent = new StringContent(str, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
            response = await _client.PostAsync("api/Account/RemoveUserRole", theContent);
            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);

            response = await _client.GetAsync("api/Account/GetUserRoles" + String.Format("?email={0}", Uri.EscapeUriString(model.User)));
            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);

        }
Example #11
0
        public async Task<IHttpActionResult> DeleteRole(DeleteRoleModel role)
        {
            CustomRole r = RoleManager.FindByName(role.DeleteRole);
            IdentityResult result = await RoleManager.DeleteAsync(r);

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

            return Ok();
        }