Ejemplo n.º 1
0
        public IActionResult Delete(long id)
        {
            var      user               = _UserManager.Get(id);
            var      userRole           = user.UserTypeUser.OrderBy(it => it.UserType.Priority).Last();
            UserType maxCurrentUserType = UserType.GetMaxUserType((User.FindFirstValue(ClaimTypes.Role) ?? "").Split(","));

            if (user != null)
            {
                if (user.Username == User.FindFirstValue(ClaimTypes.NameIdentifier))
                {
                    return(Json(new { success = false, responseText = "You cannot remove yourself!" }));
                }
                else if (userRole != null && maxCurrentUserType != null && UserType.CompareRole(maxCurrentUserType.UserTypeName, userRole.UserType.UserTypeName) < 0)
                {
                    return(Json(new { success = false, responseText = "You do not have sufficient authority to delete this account!" }));
                }
                else
                {
                    _UserManager.Delete(user);
                    user.HashPassword = "";
                    var uploads = Path.Combine(host.GetContentPathRootForUploadUtils(), NameUtils.ControllerName <UploadsController>().ToLower(), user.Username.ToLower());
                    // Xóa thư mục tệp tin của người dùng này nếu có tồn tại
                    if (Directory.Exists(uploads))
                    {
                        Directory.Delete(uploads, true);
                    }
                    return(Json(new { success = true, user = JsonConvert.SerializeObject(user), responseText = "Deleted" }));
                }
            }
            else
            {
                return(Json(new { success = false, responseText = "Can not find this user!" }));
            }
        }