From 515119c56db2870f69bf407da2939d4479be8670 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 10 Apr 2013 13:44:08 +0530 Subject: [PATCH 1/6] [fixes] auto addition of amended_from and file_list field --- core/doctype/doctype/doctype.py | 65 +++++++++++++++------------------ 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/core/doctype/doctype/doctype.py b/core/doctype/doctype/doctype.py index 1fcf757b66..065f0b872c 100644 --- a/core/doctype/doctype/doctype.py +++ b/core/doctype/doctype/doctype.py @@ -144,46 +144,41 @@ class DocType: if allow_attach is checked and the column file_list doesn't exist, create a new field 'file_list' """ - if self.doc.allow_attach and not self.doc.fields.get("__islocal"): - import webnotes.model.doctype - temp_doclist = webnotes.model.doctype.get(self.doc.name) - if 'file_list' not in [d.fieldname for d in temp_doclist if \ - d.doctype=='DocField']: - new = self.doc.addchild('fields', 'DocField', self.doclist) - new.label = 'File List' - new.fieldtype = 'Text' - new.fieldname = 'file_list' - new.hidden = 1 - new.permlevel = 0 - new.print_hide = 1 - new.no_copy = 1 - idx_list = [d.idx for d in temp_doclist if d.idx] - max_idx = idx_list and max(idx_list) or 0 - new.idx = max_idx + 1 + if self.doc.allow_attach: + if not webnotes.conn.sql("""select name from tabDocField + where fieldname = 'file_list' and parent = %s""", self.doc.name): + new = self.doc.addchild('fields', 'DocField', self.doclist) + new.label = 'File List' + new.fieldtype = 'Text' + new.fieldname = 'file_list' + new.hidden = 1 + new.permlevel = 0 + new.print_hide = 1 + new.no_copy = 1 + new.idx = self.get_max_idx() + 1 def make_amendable(self): """ - if is_submittable is set, add amended_from - docfields + if is_submittable is set, add amended_from docfields """ if self.doc.is_submittable: - import webnotes.model.doctype - temp_doclist = webnotes.model.doctype.get(self.doc.name) - max_idx = max([d.idx for d in temp_doclist if d.idx]) - max_idx = max_idx and max_idx or 0 - if 'amended_from' not in [d.fieldname for d in temp_doclist if \ - d.doctype=='DocField']: - new = self.doc.addchild('fields', 'DocField', self.doclist) - new.label = 'Amended From' - new.fieldtype = 'Link' - new.fieldname = 'amended_from' - new.options = self.doc.name - new.permlevel = 0 - new.read_only = 1 - new.print_hide = 1 - new.no_copy = 1 - new.idx = max_idx + 1 - max_idx += 1 + if not webnotes.conn.sql("""select name from tabDocField + where fieldname = 'amended_from' and parent = %s""", self.doc.name): + new = self.doc.addchild('fields', 'DocField', self.doclist) + new.label = 'Amended From' + new.fieldtype = 'Link' + new.fieldname = 'amended_from' + new.options = self.doc.name + new.permlevel = 0 + new.read_only = 1 + new.print_hide = 1 + new.no_copy = 1 + new.idx = self.get_max_idx() + 1 + + def get_max_idx(self): + max_idx = webnotes.conn.sql("""select max(idx) from `tabDocField` where parent = %s""", + self.doc.name) + return max_idx and max_idx[0][0] or 0 def validate_fields_for_doctype(doctype): from webnotes.model.doctype import get From 7d214affee4885feaefd51c8b6084937e02cf430 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 10 Apr 2013 14:41:33 +0530 Subject: [PATCH 2/6] [module visibility] [fixes] --- public/js/wn/misc/user.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/public/js/wn/misc/user.js b/public/js/wn/misc/user.js index 317da9ce4a..e40dd1751c 100644 --- a/public/js/wn/misc/user.js +++ b/public/js/wn/misc/user.js @@ -54,17 +54,18 @@ $.extend(wn.user, { } }); } + + if(!modules_list || !modules_list.length) { + // all modules + modules_list = keys(wn.modules).sort(); + } + // filter hidden modules if(wn.boot.hidden_modules && modules_list) { var hidden_list = JSON.parse(wn.boot.hidden_modules); var modules_list = $.map(modules_list, function(m) { if(hidden_list.indexOf(m)==-1) return m; else return null; }); - } - - if(!modules_list || !modules_list.length) { - // all modules - modules_list = keys(wn.modules).sort(); } // hide based on permission From e0f8cf3e8a19a14d761b3fb5ee187c7c2342ad92 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 10 Apr 2013 15:27:52 +0530 Subject: [PATCH 3/6] [permission manager] [fix] if match exists of type fieldname:user and no user property exists for that fieldname, set user default for that fieldname as webnotes.session.user when loading defaults --- .../permission_manager/permission_manager.py | 11 +++++++++++ webnotes/defaults.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/core/page/permission_manager/permission_manager.py b/core/page/permission_manager/permission_manager.py index 236a9cd84b..25e4b9846d 100644 --- a/core/page/permission_manager/permission_manager.py +++ b/core/page/permission_manager/permission_manager.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals import webnotes +import webnotes.defaults @webnotes.whitelist(allow_roles=["System Manager", "Administrator"]) def get_roles_and_doctypes(): @@ -22,8 +23,13 @@ def get_permissions(doctype=None, role=None): @webnotes.whitelist(allow_roles=["System Manager", "Administrator"]) def remove(doctype, name): + match = webnotes.conn.get_value("DocPerm", name, "match") + webnotes.conn.sql("""delete from tabDocPerm where name=%s""", name) validate_and_reset(doctype, for_remove=True) + + if match: + webnotes.defaults.clear_cache() @webnotes.whitelist(allow_roles=["System Manager", "Administrator"]) def add(parent, role, permlevel): @@ -46,11 +52,15 @@ def update(name, doctype, ptype, value=0): % (ptype, '%s', '%s'), (value, name)) validate_and_reset(doctype) + if ptype == "read" and webnotes.conn.get_value("DocPerm", name, "match"): + webnotes.defaults.clear_cache() + @webnotes.whitelist(allow_roles=["System Manager", "Administrator"]) def update_match(name, doctype, match=""): webnotes.conn.sql("""update tabDocPerm set `match`=%s where name=%s""", (match, name)) validate_and_reset(doctype) + webnotes.defaults.clear_cache() def validate_and_reset(doctype, for_remove=False): from core.doctype.doctype.doctype import validate_permissions_for_doctype @@ -61,6 +71,7 @@ def validate_and_reset(doctype, for_remove=False): def reset(doctype): webnotes.reset_perms(doctype) webnotes.clear_cache(doctype=doctype) + webnotes.defaults.clear_cache() @webnotes.whitelist(allow_roles=["System Manager", "Administrator"]) def get_users_with_role(role): diff --git a/webnotes/defaults.py b/webnotes/defaults.py index 1fa9f27aa4..430eb803b4 100644 --- a/webnotes/defaults.py +++ b/webnotes/defaults.py @@ -115,11 +115,29 @@ def get_defaults_for(parent="Control Panel"): defaults[d.defkey].append(d.defvalue) else: defaults[d.defkey] = d.defvalue + + if parent == webnotes.session.user: + defaults.update(get_defaults_for_match(defaults)) webnotes.cache().set_value("__defaults:" + parent, defaults) return defaults +def get_defaults_for_match(userd): + """ if a profile based match condition exists for a user's role + and no user property is specified for that match key, + set default value as user's profile for that match key""" + user_roles = webnotes.get_roles() + out = {} + + for role, match in webnotes.conn.sql("""select distinct role, `match` + from `tabDocPerm` where ifnull(permlevel, 0)=0 and `read`=1 + and `match` like "%:user" """): + if role in user_roles and match.split(":")[0] not in userd: + out[match.split(":")[0]] = webnotes.session.user + + return out + def clear_cache(parent=None): def all_profiles(): return webnotes.conn.sql_list("select name from tabProfile") + ["Control Panel"] From 1bce74457628a9dd218c2ec91b3b5b99e9527310 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 10 Apr 2013 18:30:02 +0530 Subject: [PATCH 4/6] [fixes] changes in permission validation message --- webnotes/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/webnotes/__init__.py b/webnotes/__init__.py index f6badd934c..fb41c77872 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -275,8 +275,12 @@ def has_permission(doctype, ptype="read", doc=None): # no valid permission found if match_failed: + from utils import cstr key = match_failed.keys()[0] - msgprint(_("Not allowed for: ") + key + "=" + match_failed[key]) + msg = _("Not allowed for: ") + for key in match_failed: + msg += "\n" + key + " = " + (match_failed[key] or "None") + msgprint(msg) return False else: return perms and True or False From a0eca348e28b27dba2fe966d1621549cdbe488c6 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 10 Apr 2013 18:30:31 +0530 Subject: [PATCH 5/6] [fixes] changes in permission validation message --- webnotes/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/webnotes/__init__.py b/webnotes/__init__.py index fb41c77872..484c29e8b8 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -275,8 +275,6 @@ def has_permission(doctype, ptype="read", doc=None): # no valid permission found if match_failed: - from utils import cstr - key = match_failed.keys()[0] msg = _("Not allowed for: ") for key in match_failed: msg += "\n" + key + " = " + (match_failed[key] or "None") From db7559dd80da165c1383ddcc1b62d945fce2216e Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 10 Apr 2013 20:27:57 +0530 Subject: [PATCH 6/6] [defaults] [fix] if session is set, only then set get_defaults_for_match --- webnotes/defaults.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webnotes/defaults.py b/webnotes/defaults.py index 430eb803b4..8c9c0ba265 100644 --- a/webnotes/defaults.py +++ b/webnotes/defaults.py @@ -116,7 +116,7 @@ def get_defaults_for(parent="Control Panel"): else: defaults[d.defkey] = d.defvalue - if parent == webnotes.session.user: + if webnotes.session and parent == webnotes.session.user: defaults.update(get_defaults_for_match(defaults)) webnotes.cache().set_value("__defaults:" + parent, defaults)