mirror of
https://github.com/donl/gPanel.git
synced 2026-05-31 22:07:25 -06:00
changing password of users now works in server
This commit is contained in:
parent
b9256cc335
commit
7f9136cf2a
6 changed files with 232 additions and 5 deletions
63
pkg/api/user/update_password.go
Normal file
63
pkg/api/user/update_password.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/Ennovar/gPanel/pkg/database"
|
||||
"github.com/Ennovar/gPanel/pkg/encryption"
|
||||
)
|
||||
|
||||
func UpdatePassword(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool {
|
||||
if req.Method != "UPDATE" {
|
||||
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed))
|
||||
http.Error(res, req.Method+" HTTP method is unsupported for this API.", http.StatusMethodNotAllowed)
|
||||
return false
|
||||
}
|
||||
|
||||
var updatePasswordRequestData struct {
|
||||
User string `json:"user"`
|
||||
Pass string `json:"pass"`
|
||||
}
|
||||
|
||||
err := json.NewDecoder(req.Body).Decode(&updatePasswordRequestData)
|
||||
if err != nil {
|
||||
logger.Println(req.URL.Path + "::" + err.Error())
|
||||
http.Error(res, err.Error(), http.StatusBadRequest)
|
||||
return false
|
||||
}
|
||||
|
||||
ds, err := database.Open(dir + database.DB_MAIN)
|
||||
if err != nil || ds == nil {
|
||||
logger.Println(req.URL.Path + "::" + err.Error())
|
||||
http.Error(res, err.Error(), http.StatusInternalServerError)
|
||||
return false
|
||||
}
|
||||
defer ds.Close()
|
||||
|
||||
var userDatabaseData struct {
|
||||
Pass string `json:"pass"`
|
||||
Secret string `json:"secret"`
|
||||
}
|
||||
|
||||
userDatabaseData.Pass, err = encryption.HashPassword(updatePasswordRequestData.Pass)
|
||||
if err != nil {
|
||||
logger.Println(req.URL.Path + "::" + err.Error())
|
||||
http.Error(res, err.Error(), http.StatusInternalServerError)
|
||||
return false
|
||||
}
|
||||
|
||||
userDatabaseData.Secret = ""
|
||||
|
||||
err = ds.Put(database.BUCKET_USERS, []byte(updatePasswordRequestData.User), userDatabaseData)
|
||||
if err != nil {
|
||||
logger.Println(req.URL.Path + "::" + err.Error())
|
||||
http.Error(res, err.Error(), http.StatusInternalServerError)
|
||||
return false
|
||||
}
|
||||
|
||||
res.WriteHeader(http.StatusNoContent)
|
||||
return true
|
||||
}
|
||||
|
|
@ -76,6 +76,8 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (b
|
|||
return true, user.List(res, req, con.APILogger, con.Directory)
|
||||
case "/user/delete":
|
||||
return true, user.Delete(res, req, con.APILogger, con.Directory)
|
||||
case "/user/update_password":
|
||||
return true, user.UpdatePassword(res, req, con.APILogger, con.Directory)
|
||||
case "/bundle/create":
|
||||
return true, bundle.Create(res, req, con.APILogger, con.Bundles)
|
||||
case "/bundle/list":
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ jQuery(document).on('click', '._js_user-management-delete', function(e){
|
|||
|
||||
if(!jQuery(this).attr('data') || jQuery(this).attr('data') == "") {
|
||||
alert("An error has occurred, please refresh and try again. If problem persists please contact your administrator.");
|
||||
return;
|
||||
}
|
||||
|
||||
var ensure = confirm('Are you sure you want to delete the user "' + jQuery(this).attr('data') + '"?');
|
||||
|
|
|
|||
|
|
@ -43,17 +43,23 @@ jQuery('._js_add-user-form').on('submit', function(e){
|
|||
|
||||
jQuery('._js_add-user-generate-password').on('click', function(e){
|
||||
e.preventDefault();
|
||||
|
||||
var genpass = generatePassword();
|
||||
|
||||
toggleShowPassword(true);
|
||||
passwordInput.prop('value', genpass);
|
||||
passwordInputRetype.prop('value', genpass);
|
||||
});
|
||||
|
||||
function generatePassword() {
|
||||
var gen = "";
|
||||
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-[]:;<>?";
|
||||
|
||||
for (var i = 0; i < 32; i++) {
|
||||
gen += chars.charAt(Math.floor(Math.random() * chars.length));
|
||||
}
|
||||
|
||||
toggleShowPassword(true);
|
||||
passwordInput.prop('value', gen);
|
||||
passwordInputRetype.prop('value', gen);
|
||||
});
|
||||
return gen
|
||||
}
|
||||
|
||||
jQuery('._js_user-management-show-password').on('change', function(e){
|
||||
e.preventDefault();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,107 @@
|
|||
var userModal = jQuery('.user-management-modal');
|
||||
var newPassModal = jQuery('.new-pass-modal');
|
||||
|
||||
var newPassword = jQuery('#updatePassword');
|
||||
var newPasswordRetype = jQuery('#updatePasswordRetype');
|
||||
var newPasswordUsername = jQuery('#updatePasswordUsername');
|
||||
|
||||
jQuery(document).on('click', '._js_user-management-new-password', function(e){
|
||||
e.preventDefault();
|
||||
|
||||
if(!jQuery(this).attr('data') || jQuery(this).attr('data') == "") {
|
||||
alert("An error has occurred, please refresh and try again. If problem persists please contact your administrator.");
|
||||
return;
|
||||
}
|
||||
|
||||
var username = jQuery(this).attr('data');
|
||||
newPasswordUsername.attr('value', username);
|
||||
|
||||
newPassModal.find('.modal-title').html('Changing password for "'+username+'"');
|
||||
toggleShowPasswordNewPassword(false);
|
||||
|
||||
userModal.modal('hide');
|
||||
newPassModal.modal('show');
|
||||
});
|
||||
|
||||
jQuery('._js_back-to-user-management').on('click', function(e){
|
||||
e.preventDefault();
|
||||
|
||||
newPassModal.modal('hide');
|
||||
userModal.modal('show');
|
||||
});
|
||||
|
||||
jQuery('._js_update-password-form').on('submit', function(e){
|
||||
e.preventDefault();
|
||||
|
||||
if((newPassword && newPassword.val()) && (newPasswordRetype && newPasswordRetype.val()) && (newPasswordUsername && newPasswordUsername.val())) {
|
||||
if(newPassword.val() == newPasswordRetype.val()) {
|
||||
var ensure = confirm("Are you sure you want to change the password of user \"" + newPasswordUsername.val() + "\"?");
|
||||
if(ensure) {
|
||||
var requestData = {};
|
||||
requestData["user"] = newPasswordUsername.val();
|
||||
requestData["pass"] = newPassword.val();
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open(jQuery(this).attr('method'), jQuery(this).attr('action'), true);
|
||||
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
||||
xhr.send(JSON.stringify(requestData));
|
||||
|
||||
xhr.onloadend = function() {
|
||||
if(xhr.status == 204) {
|
||||
alert("Password successfully updated.");
|
||||
newPassModal.modal('hide');
|
||||
userModal.modal('show');
|
||||
}
|
||||
else {
|
||||
if(xhr.response != undefined && xhr.response.length != 0) {
|
||||
alert('Error: ' + xhr.response);
|
||||
}
|
||||
else {
|
||||
alert('An error has occurred, refresh and try again. If problem persists please contact your administrator.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
alert("Passwords must match.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
alert("All fields need to be filled out");
|
||||
}
|
||||
});
|
||||
|
||||
jQuery('._js_update-password-show-password').on('change', function(e){
|
||||
e.preventDefault();
|
||||
|
||||
if(this.checked) {
|
||||
toggleShowPasswordNewPassword(true);
|
||||
}
|
||||
else {
|
||||
toggleShowPasswordNewPassword(false);
|
||||
}
|
||||
});
|
||||
|
||||
jQuery('._js_update-password-generate-password').on('click', function(e){
|
||||
e.preventDefault();
|
||||
|
||||
var genpass = generatePassword();
|
||||
|
||||
toggleShowPasswordNewPassword(true);
|
||||
newPassword.prop('value', genpass);
|
||||
newPasswordRetype.prop('value', genpass);
|
||||
});
|
||||
|
||||
function toggleShowPasswordNewPassword(show) {
|
||||
if(show) {
|
||||
jQuery('._js_update-password-show-password').prop('checked', true);
|
||||
newPassword.attr('type', 'text');
|
||||
newPasswordRetype.attr('type', 'text');
|
||||
}
|
||||
else {
|
||||
jQuery('._js_update-password-show-password').prop('checked', false);
|
||||
newPassword.attr('type', 'password');
|
||||
newPasswordRetype.attr('type', 'password');
|
||||
}
|
||||
}
|
||||
|
|
@ -231,6 +231,53 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<!-- New Pass Modal -->
|
||||
<div class="modal fade new-pass-modal" tabindex="-1" role="dialog" aria-labelledby="new-pass-modal" aria-hidden="true">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title"></h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form class="_js_update-password-form" action="api/user/update_password" method="UPDATE">
|
||||
<div class="form-group">
|
||||
<label class="sr-only" for="updatePassword">New Password</label>
|
||||
<div class="input-group">
|
||||
<div class="input-group-addon"><i class="fa fa-key" aria-hidden="true"></i></div>
|
||||
<input name="pass" type="password" class="form-control" id="updatePassword" placeholder="Password">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="sr-only" for="updatePasswordRetype">Re-type New Password</label>
|
||||
<div class="input-group">
|
||||
<div class="input-group-addon"><i class="fa fa-key" aria-hidden="true"></i></div>
|
||||
<input name="pass" type="password" class="form-control" id="updatePasswordRetype" placeholder="Re-type Password">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-check">
|
||||
<label class="form-check-label">
|
||||
<input type="checkbox" class="form-check-input _js_update-password-show-password">
|
||||
Show Password
|
||||
</label>
|
||||
</div>
|
||||
<div class="btn-group" role="group">
|
||||
<button type="submit" class="btn btn-primary">Update Password</button>
|
||||
<button type="button" class="btn btn-success _js_update-password-generate-password">Generate Strong Password</button>
|
||||
</div>
|
||||
<input type="hidden" value="" id="updatePasswordUsername">
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-warning _js_back-to-user-management">Back to User Mangement</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
|
|
@ -318,6 +365,7 @@
|
|||
<script type="text/javascript" src="assets/js/panelHandlers/users/open.js"></script>
|
||||
<script type="text/javascript" src="assets/js/panelHandlers/users/new.js"></script>
|
||||
<script type="text/javascript" src="assets/js/panelHandlers/users/delete.js"></script>
|
||||
<script type="text/javascript" src="assets/js/panelHandlers/users/new_password.js"></script>
|
||||
<!-- KEEP AT BOTTOM OF BODY TAGS -->
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue