Skip to content

Commit

Permalink
Merge pull request #183 from xdorro/thuyet
Browse files Browse the repository at this point in the history
Thuyet
  • Loading branch information
thuyetbn authored Aug 28, 2021
2 parents 14cf588 + b11d4d0 commit fac5832
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 67 deletions.
118 changes: 90 additions & 28 deletions Backend/Areas/Admin/Controllers/AccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ActionResult ChangePassword(AdminChangePasswordViewModels changePasswordV
{
var errors = new Dictionary<string, string>();
var userUpdate = users.Get(changePasswordViewModel.AccountId);
var user = (Accounts) Session["user"];
var user = (Accounts)Session["user"];
foreach (var k in ModelState.Keys)
foreach (var err in ModelState[k].Errors)
{
Expand All @@ -79,21 +79,19 @@ public ActionResult ChangePassword(AdminChangePasswordViewModels changePasswordV

if (userUpdate.AccountId == 1 && user.AccountId != 1)
{
errors.Add("NewPassword", "Unauthorized");
return Json(new
{
data = errors,
data = "Unauthorized",
statusCode = 400,
message = "Error",
}, JsonRequestBehavior.AllowGet);
}

if (userUpdate.RoleId == 1 && user.RoleId != 1)
{
errors.Add("NewPassword", "Unauthorized");
return Json(new
{
data = errors,
data = "Unauthorized",
statusCode = 400,
message = "Error",
}, JsonRequestBehavior.AllowGet);
Expand Down Expand Up @@ -131,6 +129,7 @@ public ActionResult Create(AccountViewModel accounts)
{
var errors = new Dictionary<string, string>();
var check = true;
var userSession = (Accounts)Session["user"];
if (!ModelState.IsValid)
return Json(new
{
Expand Down Expand Up @@ -191,11 +190,12 @@ public ActionResult Create(AccountViewModel accounts)
NumberId = accounts.NumberId,
Phone = accounts.Phone,
AttemptLogin = 0,
RoleId = accounts.RoleId,
RoleId = userSession.AccountId == 1 ? accounts.RoleId : 3,
Address = accounts.Address,
Birthday = DateTime.Parse(accounts.Birthday),
Status = ((int)AccountStatus.Actived)
};

users.Add(account);
return Json(new
{
Expand All @@ -220,6 +220,7 @@ public ActionResult Create(AccountViewModel accounts)
}, JsonRequestBehavior.AllowGet);
}


[HttpPost]
public ActionResult Edit(AccountViewModel accounts)
{
Expand Down Expand Up @@ -257,27 +258,32 @@ public ActionResult Edit(AccountViewModel accounts)

if (acc1.AccountId == 1 && user.AccountId != 1)
{
errors.Add("Status", "Unauthorized");
return Json(new
{
statusCode = 400,
message = "Error",
data = errors
}, JsonRequestBehavior.AllowGet);
data = "Unauthorized"
}, JsonRequestBehavior.AllowGet); ;
}

if (user.RoleId != 1 && acc1.RoleId == 1)
if (acc1.RoleId == 1 && user.RoleId != 1)
{
check = false;
errors.Add("Status", "Unauthorized");
return Json(new
{
statusCode = 400,
message = "Error",
data = errors
}, JsonRequestBehavior.AllowGet);
data = "Unauthorized"
}, JsonRequestBehavior.AllowGet); ;
}

if (user.AccountId == 1 && accounts.RoleId != 1)
{
return Json(new
{
statusCode = 400,
message = "Error",
data = "You can't change your role!"
}, JsonRequestBehavior.AllowGet); ;
}
if (users.CheckDuplicate(x => x.Email == accounts.Email && x.AccountId != acc1.AccountId))
{
check = false;
Expand All @@ -304,7 +310,7 @@ public ActionResult Edit(AccountViewModel accounts)

if (ModelState.IsValid && check)
{

var acc3 = users.Get(accounts.AccountId);
acc3.Name = accounts.Name;
acc3.Email = accounts.Email;
Expand All @@ -316,7 +322,6 @@ public ActionResult Edit(AccountViewModel accounts)
{
acc3.RoleId = accounts.RoleId;
}
acc3.Status = accounts.Status;
acc3.AttemptLogin = accounts.Status == (int)AccountStatus.Actived ? 0 : 3;
if (!users.Edit(acc3))
{
Expand Down Expand Up @@ -363,7 +368,7 @@ public ActionResult Delete(int id)
message = "Error"
}, JsonRequestBehavior.AllowGet);
}

using (var _context = new ApplicationDbContext())
{
var user = _context.Accounts.FirstOrDefault(x => x.AccountId == id);
Expand All @@ -388,17 +393,8 @@ public ActionResult Delete(int id)
message = "Success"
}, JsonRequestBehavior.AllowGet);
}

{
return Json(new
{
statusCode = 400,
data = "You cannot delete your own account",
message = "Error"
}, JsonRequestBehavior.AllowGet);
}
}

if (users.Delete(id))
{
return Json(new
Expand Down Expand Up @@ -427,5 +423,71 @@ public ActionResult ProfileAccount(int id)
var data = new AccountViewModel(x);
return View(data);
}
[HttpPost]
public ActionResult ChangeStatus(int id)
{
var userSession = (Accounts)Session["user"];
if (!CheckValidate(id))
{
return Json(new
{
statusCode = 400,
message = "Unathorzied",
data = "Unathorzied"
}, JsonRequestBehavior.AllowGet);
}
if (userSession.AccountId == id)
{
return Json(new
{
statusCode = 400,
message = "Unathorzied",
data = "You can't change your status yourself"
}, JsonRequestBehavior.AllowGet);
}
var user = users.Get(id);
if (user.Status == (int)AccountStatus.Actived)
{
user.Status = (int)AccountStatus.Locked;
}
else
{
user.Status = (int)AccountStatus.Actived;
}
if (users.Edit(user))
{
return Json(new
{
statusCode = 200,
message = "Success"
}, JsonRequestBehavior.AllowGet);
}
return Json(new
{
statusCode = 400,
message = "Error"
}, JsonRequestBehavior.AllowGet);
}
private bool CheckValidate(int id)
{
var userSession = (Accounts)Session["user"];
var user = users.Get(id);
switch (userSession.RoleId)
{
case 1:
if (user.AccountId == 1)
{
return false;
}
break;
case 2:
if (user.RoleId == 1)
{
return false;
}
break;
}
return true;
}
}
}
86 changes: 50 additions & 36 deletions Backend/Areas/Admin/Views/Accounts/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -103,36 +103,15 @@
</div>
<div class="form-group">
<div class="row">
@if (user.RoleId == 1)
@if (user.RoleId == 1 && user.AccountId == 1)
{


<div class="col-md-6">
<div class="col-md-12">
<label>Role <span class="text-danger">*</span></label>
<div>
<select class="form-control" id="RoleId" name="RoleId">
</select>
</div>
</div>


<div class="col-md-6">
<label>Status <span class="text-danger">*</span></label>
<div>
<select class="form-control" id="Status" name="Status">
</select>
</div>
</div>
}
else
{
<div class="col-md-12">
<label>Status <span class="text-danger">*</span></label>
<div>
<select class="form-control" id="Status" name="Status">
</select>
</div>
</div>
}
</div>
</div>
Expand Down Expand Up @@ -216,9 +195,6 @@
"RoleId": {
required: true
},
"Status": {
required: true
},
},
messages: {
Birthday: {
Expand Down Expand Up @@ -247,7 +223,6 @@
Phone: $("#Phone").val(),
NumberId: $("#NumberId").val(),
RoleId: $("#RoleId").val(),
Status: parseInt(($("#Status").val()))
};
if ($("#type").val() === "EDIT") {
user.AccountId = $("#Id").val();
Expand Down Expand Up @@ -284,8 +259,8 @@
submitHandler: function () {
let account = {
AccountId: $("#Id2").val(),
Password: $("#NewPassword").val(),
RePassword: $("#ConfirmPassword").val(),
NewPassword: $("#NewPassword").val(),
ConfirmPassword: $("#ConfirmPassword").val(),
};
$.ajax({
type: "POST",
Expand All @@ -298,7 +273,11 @@
notifySuccess("Success", "Password Change Success");
$("#PasswordChange").modal("hide");
} else {
notifyError("Error", "Password Change Error");
if (typeof res.data == "string") {
notifyError("Error", res.data);
$("#PasswordChange").modal("hide");
}
validator2.showErrors(res.data);
}
}
Expand Down Expand Up @@ -364,8 +343,34 @@
notifySuccess('Updated Successfully',"Updated Successfully");
$("#myModal").modal("hide");
$('#datatables').DataTable().ajax.reload();
}else {
validator.showErrors(res.data);
} else {
if (typeof res.data == "string") {
$("#myModal").modal("hide");
notifyError('Updated Error', res.data);
} else {
validator.showErrors(res.data);
}
}
}
})
},
put2: function (id) {
$.ajax({
type: "POST",
url: "@Url.Action("ChangeStatus", "Accounts")",
data: { id :id },
success: function (res) {
if (res.statusCode === 200) {
notifySuccess('Updated Successfully',"Updated Successfully");
$("#myModal").modal("hide");
$('#datatables').DataTable().ajax.reload();
} else {
if (typeof res.data == "string") {
notifyError('Updated Error', res.data);
}
validator2.showErrors(res.data);
}
}
Expand Down Expand Up @@ -446,7 +451,7 @@
"orderable": false,
},
{
data: {Status: Status,StatusName:'StatusName'},
data: {Status: "Status",StatusName:'StatusName'},
render: function (data) {
if (data.Status === 0){
return '<span class="badge light badge-success"><i class="fa fa-circle text-success mr-1"></i>'+data.StatusName+'</span>';
Expand All @@ -462,13 +467,15 @@
"orderable": false,
},
{
data: {AccountId:'AccountId'},
"render": function (data) {
data: { AccountId: 'AccountId', Status:"Status"},
render: function (data) {
let toggle = data.Status === 0 ? "Lock Account" : "Active Account";
let detail = "@Url.Action("ProfileAccount", "Accounts")/"+data.AccountId;
return '<div class="dropdown custom-dropdown mb-0"><div class="btn sharp btn-primary tp-btn" data-toggle="dropdown" aria-expanded="false"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="18px" height="18px" viewBox="0 0 24 24" version="1.1"><g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"><rect x="0" y="0" width="24" height="24"></rect><circle fill="#000000" cx="12" cy="5" r="2"></circle><circle fill="#000000" cx="12" cy="12" r="2"></circle><circle fill="#000000" cx="12" cy="19" r="2"></circle></g></svg></div><div class="dropdown-menu dropdown-menu-right" x-placement="bottom-end" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(40px, 41px, 0px);">' +
'<a data-id=" ' + data.AccountId +' " class="dropdown-item btn-changepass text-info" href="javascript:void(0);">Change Password</a>' +
'<a href="' + detail +'" class="dropdown-item btn-detail text-primary" >Details</a>' +
'<a data-id=" ' + data.AccountId +' " class="dropdown-item btn-edit text-warning" href="javascript:void(0);">Edit</a>' +
'<a data-id=" ' + data.AccountId + ' " class="dropdown-item btn-edit text-warning" href="javascript:void(0);">Edit</a>' +
'<a data-id="' + data.AccountId + '" class="dropdown-item btn-edit2 text-warning" href="javascript:void(0);">' + toggle + '</a>' +
'<a data-id=" '+ data.AccountId + '" class="dropdown-item text-danger btn-delete" href="javascript:void(0);">Delete</a></div></div>'
}
},
Expand All @@ -480,8 +487,15 @@
let userId = $(this).data("id");
let a = managerAcc.get(userId);
})
$(document).on("click", ".btn-edit2", function () {
let userId = $(this).data("id");
let a = managerAcc.put2(userId);
})
$(document).on("click", ".btn-changepass", function () {
validator2.resetForm();
$("#FormPasswordChange").trigger("reset");
$("#PasswordChange").modal("show");
let userId = $(this).data("id");
$("#Id2").val(userId);
})
Expand Down
Loading

0 comments on commit fac5832

Please sign in to comment.