390 lines
15 KiB
JavaScript
390 lines
15 KiB
JavaScript
var SysUsersManager = (function() {
|
|
|
|
function SysUsersManager(){
|
|
var _this = this;
|
|
|
|
if(CBase.dt && CBase.dt.profile){
|
|
_this.oBlank = new CBlank();
|
|
_this.oBlank.loadBlankView();
|
|
}else{
|
|
_this.oTable = new CTable("data");
|
|
_this.oTable.loadTableData();
|
|
}
|
|
}
|
|
|
|
// Rewrite CTable object callback
|
|
if(typeof CTable !== 'undefined'){
|
|
CTable.prototype.tableObjectCallback = function(oTable){
|
|
var _this = this;
|
|
|
|
oTable.find("a#iCreateQRCode").click(function(){
|
|
CBase.jsModule.CreateQRDialogBox();
|
|
});
|
|
|
|
// Clone blank action
|
|
oTable.find("a#iclone").click(function(){
|
|
CBase.message.hide(true, false);
|
|
_this.loadRecord(false, $(".dxr:not(.head) li.first div.chked").closest("ul").find("#ledit").attr("href") + "/clone:1");
|
|
});
|
|
};
|
|
}else{
|
|
$(document).bind('keydown', function(event){
|
|
if(CBase.os.win || CBase.os.mac || CBase.os.linux){
|
|
if(!event.altKey){
|
|
// Set control button
|
|
var ctrl = (event.ctrlKey || CBase.os.mac && event.metaKey);
|
|
|
|
// Ctrl + s - save form
|
|
if(event.which == CBase.iKeys.KEY_S && ctrl){
|
|
if(CBase.jsModule.oBlank.isVisible){
|
|
event.preventDefault();
|
|
CBase.jsModule.oBlank.saveBlankView();
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
SysUsersManager.prototype.CreateQRDialogBox = function(){
|
|
|
|
var jqObj = $("#qr_code").clone().css({display : "block"});
|
|
|
|
jqObj.find("input").focusin(
|
|
function(){
|
|
$(this).parent().addClass('act');
|
|
}).focusout(
|
|
function(){
|
|
if(!$(this).val().length)
|
|
$(this).parent().removeClass('act');
|
|
});
|
|
|
|
|
|
var oData = {};
|
|
CBase.oDialog.jqDialog.removeAttr("class");
|
|
CBase.oDialog.jqDialog.addClass("form-data");
|
|
CBase.oDialog.show({
|
|
closeBtn: true,
|
|
title : $.isDefined(CBase.gM.sCreateQRTitle)?CBase.gM.sCreateQRTitle:"Create QR code",
|
|
message : jqObj,
|
|
buttons : {
|
|
1 : {
|
|
lbl : $.isDefined(CBase.gM.sGenQR)?CBase.gM.sGenQR:"Create code",
|
|
id : "buttonYes",
|
|
click : function(){
|
|
|
|
jqObj.find("input").each(function(){
|
|
oData[$(this).attr("id")] = $(this).val();
|
|
});
|
|
|
|
CBase.ajax({action : "gen2FA-QRCode", data : $.toJSON(oData)}, false, function(d){
|
|
if(d && d.sUrl)
|
|
jqObj.find("img").prop('src', d.sUrl);
|
|
|
|
});
|
|
|
|
}
|
|
},
|
|
2 : {
|
|
lbl : $.isDefined(CBase.gM.sClearCancel)?CBase.gM.sClearCancel:"Cancel",
|
|
id : "buttonNo",
|
|
click : function(){
|
|
CBase.oDialog.hide();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
// Save blank data callback function
|
|
CBlank.prototype.saveBlankViewCallback = function(d){
|
|
if(d && d.jsonok){
|
|
if(d.url) window.location = d.url;
|
|
this.jqBlank.find("input:password").val("");
|
|
}
|
|
};
|
|
|
|
// Bind additional events
|
|
SysUsersManager.prototype.bindLoadableContentActions = function(jqObj){
|
|
|
|
// Bind collapse / expand
|
|
jqObj.find("ul.table-fields div.title").click(function(e){
|
|
$(this).toggleClass("exp");
|
|
var ctrl = (e.ctrlKey || CBase.os.mac && e.metaKey);
|
|
if(ctrl){
|
|
if($(this).hasClass("exp")){
|
|
$(this).closest("ul.table-fields").find("div.title").addClass("exp").next().slideDown("fast");
|
|
}else{
|
|
$(this).closest("ul.table-fields").find("div.title").removeClass("exp").next().slideUp("fast");
|
|
}
|
|
}else{
|
|
if($(this).hasClass("exp")){
|
|
$(this).next().slideDown("fast");
|
|
}else{
|
|
$(this).next().slideUp("fast");
|
|
}
|
|
}
|
|
});
|
|
|
|
// Bind table column sort
|
|
jqObj.find("ul.tfields").each(function(){
|
|
$(this).sortable({
|
|
axis: 'y',
|
|
helper : 'clone',
|
|
items: "> li:not(#h_checkbox, #h_buttons, .title)"
|
|
}).disableSelection();
|
|
});
|
|
|
|
// Disable checkboxes
|
|
jqObj.find("li#h_checkbox input, li#h_buttons input").attr("disabled", true);
|
|
|
|
};
|
|
|
|
// Rewrite CBase form callback
|
|
CBlank.prototype.viewCallback = function(jqObj){
|
|
var _this = this;
|
|
|
|
jqObj.find(".setting-block, #p2fa, #psettings, #ppasschange, #tablescfg, #portletscfg, #user_groups, #user_permissions, #groups, #permissions").find("h3").click(function(){
|
|
var jqParent = $(this).parent();
|
|
if(jqParent.hasClass("load"))
|
|
_this.loadBlankBlockSubContent(jqParent, function(jqObj, d){
|
|
CBase.jsModule.bindLoadableContentActions(jqObj, d);
|
|
});
|
|
else
|
|
_this.showHideSubContentPanel(jqParent);
|
|
});
|
|
|
|
this.oBtnPan.find("#fcancel").click(function(){
|
|
CBase.message.hide();
|
|
if(_this.isModiffied){
|
|
CBase.oTable.filterTableData(true);
|
|
}else{
|
|
CBase.oTable.show();
|
|
}
|
|
});
|
|
|
|
this.oBtnPan.find("#fsave").click(function(){
|
|
_this.saveBlankView();
|
|
});
|
|
|
|
// Auto pass gen switch
|
|
jqObj.find("#user_passgen").change(function(){
|
|
var jqPsCnt = $("#ppasschange");
|
|
if($(this).val()){
|
|
CBase.ajax({action: "genRandomPwd"}, false, function(d){
|
|
jqPsCnt.find("#auto-gen-pwd").find("b").html(d.key);
|
|
jqPsCnt.find("#auto-gen-pwd").stop(true, true).fadeIn("fast");
|
|
_this.showHideSubContentPanel(jqPsCnt.find("> h3").parent(), true);
|
|
jqPsCnt.find("input:password").val(d.key);
|
|
});
|
|
}else{
|
|
jqPsCnt.find("#auto-gen-pwd").stop(true, true).fadeOut("fast");
|
|
jqPsCnt.find("input:password").val("");
|
|
_this.showHideSubContentPanel(jqPsCnt.find("> h3").parent(), false);
|
|
}
|
|
});
|
|
|
|
jqObj.find("#copy_code").bind({
|
|
click : function(){
|
|
if(CBase.copyToClipboard($(this).parent().find("b").html())){
|
|
var jqNode = $(this).find(".errMsg");
|
|
jqNode.html(jqNode.attr("data-ok-msg"));
|
|
}
|
|
},
|
|
mouseenter : function(){
|
|
var jqNode = $(this).find(".errMsg");
|
|
jqNode.html(jqNode.attr("data-base-msg"));
|
|
}
|
|
});
|
|
|
|
jqObj.find("#user_theme .chk").change(function(){
|
|
jqObj.find("#user_theme .chked").not(this).removeClass("chked");
|
|
if(!jqObj.find("#user_theme .chked").length)
|
|
$(this).addClass("chked");
|
|
});
|
|
jqObj.find("#user_theme .chk").closest("li").click(function(e){
|
|
e.stopPropagation();
|
|
$(this).find(".chk").click();
|
|
});
|
|
|
|
// 2FA authenticator setup / removal
|
|
jqObj.find("#act_2fa").click(function(){
|
|
CBase.jsModule.activate2FA();
|
|
});
|
|
jqObj.find("#dis_2fa").click(function(){
|
|
CBase.jsModule.deactivate2FA();
|
|
});
|
|
|
|
// Bind pp-search
|
|
this.bindSearch(jqObj);
|
|
|
|
this.bindBlankActions(jqObj);
|
|
};
|
|
|
|
// Activate 2FA
|
|
SysUsersManager.prototype.activate2FA = function(){
|
|
|
|
var _this = this;
|
|
var sUrl = (CBase.dt && CBase.dt.profile ? _this.oBlank.url : _this.oTable.oBlank.url);
|
|
|
|
CBase.ajax({action : "get2FA-QRCode"}, sUrl, function(d){
|
|
if(d && d.html){
|
|
CBase.oDialog.show({
|
|
closeBtn: true,
|
|
title : d.msgEnableAuth ? d.msgEnableAuth : "...",
|
|
message : d.html,
|
|
buttons : {
|
|
1 : {
|
|
lbl : d.buttonCancel ? d.buttonCancel : "Cancel",
|
|
id : "buttonNo",
|
|
click : function(){
|
|
CBase.oDialog.hide();
|
|
}
|
|
},
|
|
2 : {
|
|
lbl : d.buttonYes ? d.buttonYes : "Next step",
|
|
id : "buttonYes",
|
|
click : function(jqObj){
|
|
jqObj.object.find("#buttonNo, #buttonYes, #step1").hide();
|
|
jqObj.object.find("#buttonBack, #buttonActivate2fa, #step2").fadeIn("fast");
|
|
}
|
|
},
|
|
// Step 2 buttons
|
|
3 : {
|
|
lbl : d.buttonBack ? d.buttonBack : "Go Back",
|
|
id : "buttonBack",
|
|
click : function(jqObj){
|
|
jqObj.object.find("#buttonBack, #buttonActivate2fa, #step2").hide();
|
|
jqObj.object.find("#buttonNo, #buttonYes, #step1").fadeIn("fast");
|
|
}
|
|
},
|
|
4 : {
|
|
lbl : d.buttonActivate ? d.buttonActivate : "Activate",
|
|
id : "buttonActivate2fa",
|
|
click : function(){
|
|
if(_this.o2FAValidator.isFormValid()){
|
|
CBase.ajax({
|
|
action : "activate-2FA",
|
|
secret : $("#secret").text(),
|
|
code : $("#code").val()
|
|
}, sUrl, function(d){
|
|
if(d){
|
|
if(d.error && d.data)
|
|
_this.o2FAValidator.showErrorsByObject(d.data);
|
|
if(d.jsonok){
|
|
$("#p2fa").addClass("enabled");
|
|
CBase.oDialog.hide();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
},
|
|
callbackAfterAppend : function(jqObj){
|
|
jqObj.find("#buttonBack, #buttonActivate2fa").hide();
|
|
jqObj.find("#reload-qr").click(function(){
|
|
CBase.ajax({action : "get2FA-QRCode", reload : 1}, sUrl, function(d){
|
|
if(d && d.sUrl && d.sSecret){
|
|
jqObj.find("img").prop('src', d.sUrl);
|
|
jqObj.find("#secret").html(d.sSecret);
|
|
}
|
|
});
|
|
});
|
|
jqObj.find("input").bind({
|
|
focus : function(){
|
|
$(this).parent().addClass("act");
|
|
},
|
|
blur : function(){
|
|
if(!$(this).val().length)
|
|
$(this).parent().removeClass("act");
|
|
},
|
|
keydown : function(e){
|
|
if(e.which == CBase.iKeys.ENTER)
|
|
$("#buttonActivate2fa").click();
|
|
}
|
|
});
|
|
|
|
// Bind validator
|
|
if($.isFunction(Validator) && $.isDefined(CBase.dt.vcfg)){
|
|
_this.o2FAValidator = new Validator(CBase.dt.vcfg, "#step2", CBase.gM);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
};
|
|
|
|
// Deactivate 2FA
|
|
SysUsersManager.prototype.deactivate2FA = function(){
|
|
|
|
var _this = this;
|
|
var sUrl = (CBase.dt && CBase.dt.profile ? _this.oBlank.url : _this.oTable.oBlank.url);
|
|
|
|
CBase.ajax({action : "get2FA-4Deactivate"}, sUrl, function(d){
|
|
if(d && d.html){
|
|
CBase.oDialog.show({
|
|
closeBtn: true,
|
|
title : d.msgDisableAuth ? d.msgDisableAuth : "...",
|
|
message : d.html,
|
|
buttons : {
|
|
1 : {
|
|
lbl : d.buttonCancel ? d.buttonCancel : "Cancel",
|
|
id : "buttonNo",
|
|
click : function(){
|
|
CBase.oDialog.hide();
|
|
}
|
|
},
|
|
2 : {
|
|
lbl : d.buttonDeactivate ? d.buttonDeactivate : "Deactivate",
|
|
id : "buttonDeactivate",
|
|
click : function(){
|
|
if(_this.o2FAValidator.isFormValid()){
|
|
CBase.ajax({
|
|
action : "deactivate-2FA",
|
|
code : $("#code").val()
|
|
}, sUrl, function(d){
|
|
if(d){
|
|
if(d.error && d.data)
|
|
_this.o2FAValidator.showErrorsByObject(d.data);
|
|
if(d.jsonok){
|
|
$("#p2fa").removeClass("enabled");
|
|
CBase.oDialog.hide();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
},
|
|
callbackAfterAppend : function(jqObj){
|
|
jqObj.find("input").bind({
|
|
focus : function(){
|
|
$(this).parent().addClass("act");
|
|
},
|
|
blur : function(){
|
|
if(!$(this).val().length)
|
|
$(this).parent().removeClass("act");
|
|
},
|
|
keydown : function(e){
|
|
if(e.which == CBase.iKeys.ENTER)
|
|
$("#buttonDeactivate").click();
|
|
}
|
|
});
|
|
|
|
// Bind validator
|
|
if($.isFunction(Validator) && $.isDefined(CBase.dt.vcfg)){
|
|
_this.o2FAValidator = new Validator(CBase.dt.vcfg, "#step2", CBase.gM);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
return SysUsersManager;
|
|
|
|
})();
|