Ver a proveniência

user role as child table in profile. checkboxes and user roles managed directly using addchild on client side

version-14
Anand Doshi há 12 anos
ascendente
cometimento
54d0b777a7
6 ficheiros alterados com 116 adições e 73 eliminações
  1. +65
    -29
      core/doctype/profile/profile.js
  2. +15
    -38
      core/doctype/profile/profile.py
  3. +23
    -2
      core/doctype/profile/profile.txt
  4. +8
    -1
      core/doctype/userrole/userrole.py
  5. +2
    -3
      core/doctype/userrole/userrole.txt
  6. +3
    -0
      public/js/legacy/widgets/form/form.js

+ 65
- 29
core/doctype/profile/profile.js Ver ficheiro

@@ -1,8 +1,14 @@
cur_frm.cscript.onload = function(doc) {
if(!cur_frm.roles_editor && has_common(user_roles, ["Administrator", "System Manager"])) {
var role_area = $('<div style="min-height: 300px">')
.appendTo(cur_frm.fields_dict.roles_html.wrapper);
cur_frm.roles_editor = new wn.RoleEditor(role_area);
if(has_common(user_roles, ["Administrator", "System Manager"])) {
if(!cur_frm.roles_editor) {
var role_area = $('<div style="min-height: 300px">')
.appendTo(cur_frm.fields_dict.roles_html.wrapper);
cur_frm.roles_editor = new wn.RoleEditor(role_area);
} else {
// called when creating a new profile
// and need to clear previous profile's roles
cur_frm.roles_editor.show();
}
}
}

@@ -21,7 +27,8 @@ cur_frm.cscript.refresh = function(doc) {
}
cur_frm.cscript.enabled(doc);
cur_frm.roles_editor && cur_frm.roles_editor.show(doc.name);
cur_frm.roles_editor && cur_frm.roles_editor.show();
if(user==doc.name) {
// update display settings
wn.ui.set_theme(doc.theme);
@@ -53,9 +60,7 @@ cur_frm.cscript.enabled = function(doc) {

cur_frm.cscript.validate = function(doc) {
if(cur_frm.roles_editor) {
doc.__temp = JSON.stringify({
roles:cur_frm.roles_editor.get_roles()
});
cur_frm.roles_editor.set_roles_in_table()
}
}

@@ -69,6 +74,12 @@ wn.RoleEditor = Class.extend({
callback: function(r) {
me.roles = r.message;
me.show_roles();
// refresh call could've already happened
// when all role checkboxes weren't created
if(cur_frm.doc) {
cur_frm.roles_editor.show();
}
}
});
},
@@ -90,38 +101,63 @@ wn.RoleEditor = Class.extend({
return false;
})
},
show: function(uid) {
show: function() {
var me = this;
this.uid = uid;
// set user roles
wn.call({
method:'core.doctype.profile.profile.get_user_roles',
args: {uid:uid},
callback: function(r, rt) {
$(me.wrapper).find('input[type="checkbox"]').attr('checked', false);
for(var i in r.message) {
$(me.wrapper)
.find('[data-user-role="'+r.message[i]
+'"] input[type="checkbox"]').attr('checked',true);
}
}
})
// uncheck all roles
$(this.wrapper).find('input[type="checkbox"]').removeAttr("checked");
// set user roles as checked
$.each(wn.model.get("UserRole", {parent: cur_frm.doc.name,
parentfield: "user_roles"}), function(i, user_role) {
$(me.wrapper)
.find('[data-user-role="'+user_role.role
+'"] input[type="checkbox"]').attr('checked', 'checked');
});
},
set_roles_in_table: function() {
var opts = this.get_roles();
var existing_roles_map = {};
var existing_roles_list = [];
$.each(wn.model.get("UserRole", {parent: cur_frm.doc.name,
parentfield: "user_roles"}), function(i, user_role) {
existing_roles_map[user_role.role] = user_role.name;
existing_roles_list.push(user_role.role);
});
// remove unchecked roles
$.each(opts.unchecked_roles, function(i, role) {
if(existing_roles_list.indexOf(role)!=-1) {
wn.model.clear_doc("UserRole", existing_roles_map[role]);
}
});
// add new roles that are checked
$.each(opts.checked_roles, function(i, role) {
if(existing_roles_list.indexOf(role)==-1) {
var user_role = wn.model.add_child(cur_frm.doc, "UserRole", "user_roles");
user_role.role = role;
}
});
refresh_field("user_roles");
},
get_roles: function() {
var set_roles = [];
var unset_roles = [];
var checked_roles = [];
var unchecked_roles = [];
$(this.wrapper).find('[data-user-role]').each(function() {
var $check = $(this).find('input[type="checkbox"]');
if($check.attr('checked')) {
set_roles.push($(this).attr('data-user-role'));
checked_roles.push($(this).attr('data-user-role'));
} else {
unset_roles.push($(this).attr('data-user-role'));
unchecked_roles.push($(this).attr('data-user-role'));
}
});
return {
set_roles: set_roles,
unset_roles: unset_roles
checked_roles: checked_roles,
unchecked_roles: unchecked_roles
}
},
show_permissions: function(role) {


+ 15
- 38
core/doctype/profile/profile.py Ver ficheiro

@@ -49,7 +49,7 @@ class DocType:
del self.doc.fields['__temp']

self.validate_max_users()
self.update_roles()
self.check_one_system_manager()
# do not allow disabling administrator/guest
if not cint(self.doc.enabled) and self.doc.name in ["Administrator", "Guest"]:
@@ -88,44 +88,21 @@ class DocType:
1. <b>Upgrade to the unlimited users plan</b>, or<br /> \
2. <b>Disable one or more of your existing users and try again</b>""" \
% {'active_users': active_users}, raise_exception=1)
def update_roles(self):
"""update roles if set"""

if self.temp.get('roles'):
from webnotes.model.doc import Document

# remove roles
webnotes.conn.sql("""delete from tabUserRole where parent='%s'
and role in ('%s')""" % (self.doc.name,
"','".join(self.temp['roles']['unset_roles'])))

if "System Manager" in self.temp['roles']['unset_roles']:
self.check_one_system_manager()

# add roles
user_roles = webnotes.get_roles(self.doc.name)
for role in self.temp['roles']['set_roles']:
if not role in user_roles:
self.add_role(role)
def add_role(self, role):
d = webnotes.doc('UserRole')
d.role = role
d.parenttype = 'Profile'
d.parentfield = 'user_roles'
d.parent = self.doc.name
d.save()
def check_one_system_manager(self):
if not webnotes.conn.sql("""select parent from tabUserRole where role='System Manager' and docstatus<2 and parent!='Administrator'"""):
if webnotes.conn.sql("""select count(*) from `tabProfile`
where name not in ('Administrator', 'Guest')""")[0][0] == 0:
self.temp["roles"]["set_roles"].append("System Manager")
return
webnotes.msgprint("""Cannot un-select as System Manager as there must
be atleast one 'System Manager'.""", raise_exception=1)
# if adding system manager, do nothing
if "System Manager" in [user_role.role for user_role in
self.doclist.get({"parentfield": "user_roles"})]:
return
if not webnotes.conn.sql("""select parent from tabUserRole where role='System Manager' and docstatus<2 and parent not in ('Administrator', %s)""", (self.doc.name,)):
webnotes.msgprint("""Adding System Manager Role as there must
be atleast one 'System Manager'.""")
self.doclist.append({
"doctype": "UserRole",
"parentfield": "user_roles",
"role": "System Manager"
})
def on_update(self):
# owner is always name


+ 23
- 2
core/doctype/profile/profile.txt Ver ficheiro

@@ -1,8 +1,8 @@
[
{
"creation": "2013-02-06 16:11:18",
"creation": "2013-02-11 12:30:10",
"docstatus": 0,
"modified": "2013-02-11 11:43:26",
"modified": "2013-02-13 09:35:48",
"modified_by": "Administrator",
"owner": "Administrator"
},
@@ -168,6 +168,7 @@
"fieldtype": "Password",
"hidden": 0,
"label": "New Password",
"no_copy": 1,
"print_hide": 1
},
{
@@ -377,6 +378,26 @@
"oldfieldname": "file_list",
"oldfieldtype": "Text"
},
{
"doctype": "DocField",
"fieldname": "roles_assigned_to_user",
"fieldtype": "Section Break",
"hidden": 1,
"label": "Roles Assigned To User",
"no_copy": 0,
"print_hide": 1,
"read_only": 1
},
{
"doctype": "DocField",
"fieldname": "user_roles",
"fieldtype": "Table",
"hidden": 1,
"label": "Roles Assigned",
"options": "UserRole",
"print_hide": 1,
"read_only": 1
},
{
"cancel": 1,
"create": 1,


+ 8
- 1
core/doctype/userrole/userrole.py Ver ficheiro

@@ -21,7 +21,14 @@

from __future__ import unicode_literals
import webnotes
from webnotes.utils import cint

class DocType:
def __init__(self, d, dl):
self.doc, self.doclist = d, dl
self.doc, self.doclist = d, dl
def validate(self):
if cint(self.doc.fields.get("__islocal")) and webnotes.conn.exists("UserRole", {
"parent": self.doc.parent, "role": self.doc.role}):
webnotes.msgprint("Role Already Exists", raise_exception=True)

+ 2
- 3
core/doctype/userrole/userrole.txt Ver ficheiro

@@ -1,8 +1,8 @@
[
{
"creation": "2013-01-10 16:34:04",
"creation": "2013-02-06 11:30:13",
"docstatus": 0,
"modified": "2013-02-06 11:43:05",
"modified": "2013-02-13 07:51:57",
"modified_by": "Administrator",
"owner": "Administrator"
},
@@ -12,7 +12,6 @@
"allow_print": 0,
"autoname": "UR.#####",
"doctype": "DocType",
"document_type": "Master",
"hide_heading": 0,
"hide_toolbar": 0,
"issingle": 0,


+ 3
- 0
public/js/legacy/widgets/form/form.js Ver ficheiro

@@ -898,6 +898,9 @@ _f.Frm.prototype.save = function(save_action, callback, btn, on_error) {
$(document.activeElement).blur();
var me = this;
if((!this.meta.in_dialog || this.in_form) && !this.meta.istable)
scroll(0, 0);
// validate
if(save_action!="Cancel") {
validated = true;


Carregando…
Cancelar
Guardar