You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

209 line
6.5 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import webnotes, json
  24. class Profile:
  25. """
  26. A profile object is created at the beginning of every request with details of the use.
  27. The global profile object is `webnotes.user`
  28. """
  29. def __init__(self, name=''):
  30. self.name = name or webnotes.session.get('user')
  31. self.roles = []
  32. self.all_read = []
  33. self.can_create = []
  34. self.can_read = []
  35. self.can_write = []
  36. self.can_cancel = []
  37. self.can_search = []
  38. self.can_get_report = []
  39. self.allow_modules = []
  40. # for doctypes with create permission but are not supposed to be created using New
  41. self.in_create = []
  42. def get_roles(self):
  43. """get list of roles"""
  44. if not self.roles:
  45. self.roles = webnotes.get_roles()
  46. return self.roles
  47. def build_doctype_map(self):
  48. """build map of special doctype properties"""
  49. self.doctype_map = {}
  50. for r in webnotes.conn.sql("""select name, in_create, issingle, istable,
  51. read_only, module from tabDocType""", as_dict=1):
  52. r['child_tables'] = []
  53. self.doctype_map[r['name']] = r
  54. for r in webnotes.conn.sql("""select parent, options from tabDocField
  55. where fieldtype="Table"
  56. and parent not like "old_parent:%%"
  57. and ifnull(docstatus,0)=0
  58. """):
  59. if r[0] in self.doctype_map:
  60. self.doctype_map[r[0]]['child_tables'].append(r[1])
  61. def build_perm_map(self):
  62. """build map of permissions at level 0"""
  63. self.perm_map = {}
  64. for r in webnotes.conn.sql("""select parent, `read`, `write`, `create`, `submit`, `cancel`
  65. from tabDocPerm where docstatus=0
  66. and ifnull(permlevel,0)=0
  67. and parent not like "old_parent:%%"
  68. and role in ('%s')""" % "','".join(self.get_roles()), as_dict=1):
  69. dt = r['parent']
  70. if not dt in self.perm_map:
  71. self.perm_map[dt] = {}
  72. for k in ('read', 'write', 'create', 'submit', 'cancel'):
  73. if not self.perm_map[dt].get(k):
  74. self.perm_map[dt][k] = r.get(k)
  75. def build_permissions(self):
  76. """build lists of what the user can read / write / create
  77. quirks:
  78. read_only => Not in Search
  79. in_create => Not in create
  80. """
  81. self.build_doctype_map()
  82. self.build_perm_map()
  83. for dt in self.doctype_map:
  84. dtp = self.doctype_map[dt]
  85. p = self.perm_map.get(dt, {})
  86. if not dtp.get('istable'):
  87. if p.get('create') and not dtp.get('issingle'):
  88. if dtp.get('in_create'):
  89. self.in_create.append(dt)
  90. else:
  91. self.can_create.append(dt)
  92. elif p.get('write'):
  93. self.can_write.append(dt)
  94. elif p.get('read'):
  95. if dtp.get('read_only'):
  96. self.all_read.append(dt)
  97. else:
  98. self.can_read.append(dt)
  99. if p.get('cancel'):
  100. self.can_cancel.append(dt)
  101. if (p.get('read') or p.get('write') or p.get('create')):
  102. self.can_get_report.append(dt)
  103. self.can_get_report += dtp['child_tables']
  104. if not dtp.get('istable'):
  105. if not dtp.get('issingle') and not dtp.get('read_only'):
  106. self.can_search.append(dt)
  107. if not dtp.get('module') in self.allow_modules:
  108. self.allow_modules.append(dtp.get('module'))
  109. self.can_write += self.can_create
  110. self.can_write += self.in_create
  111. self.can_read += self.can_write
  112. self.all_read += self.can_read
  113. def get_defaults(self):
  114. if not self.defaults:
  115. roles = self.get_roles() + [self.name]
  116. res = webnotes.conn.sql("""select defkey, defvalue
  117. from `tabDefaultValue` where parent in ("%s") order by idx""" % '", "'.join(roles))
  118. self.defaults = {'owner': [self.name], "user": [self.name]}
  119. for rec in res:
  120. if not self.defaults.has_key(rec[0]):
  121. self.defaults[rec[0]] = []
  122. self.defaults[rec[0]].append(rec[1] or '')
  123. return self.defaults
  124. # update recent documents
  125. def update_recent(self, dt, dn):
  126. rdl = webnotes.cache().get_value("recent:" + self.name) or []
  127. new_rd = [dt, dn]
  128. # clear if exists
  129. for i in range(len(rdl)):
  130. rd = rdl[i]
  131. if rd==new_rd:
  132. del rdl[i]
  133. break
  134. if len(rdl) > 19:
  135. rdl = rdl[:19]
  136. rdl = [new_rd] + rdl
  137. r = webnotes.cache().set_value("recent:" + self.name, rdl)
  138. def load_profile(self):
  139. d = webnotes.conn.sql("""select email, first_name, last_name,
  140. email_signature, theme, background_image
  141. from tabProfile where name = %s""", self.name, as_dict=1)[0]
  142. if not self.can_read:
  143. self.build_permissions()
  144. d.name = self.name
  145. d.recent = json.dumps(webnotes.cache().get_value("recent:" + self.name) or [])
  146. if not d.theme:
  147. d.theme = "Default"
  148. d['roles'] = self.get_roles()
  149. d['defaults'] = self.get_defaults()
  150. d['can_create'] = self.can_create
  151. d['can_write'] = self.can_write
  152. d['can_read'] = list(set(self.can_read))
  153. d['can_cancel'] = list(set(self.can_cancel))
  154. d['can_get_report'] = list(set(self.can_get_report))
  155. d['allow_modules'] = self.allow_modules
  156. d['all_read'] = self.all_read
  157. d['can_search'] = list(set(self.can_search))
  158. d['in_create'] = self.in_create
  159. return d
  160. def get_user_fullname(user):
  161. fullname = webnotes.conn.sql("SELECT CONCAT_WS(' ', first_name, last_name) FROM `tabProfile` WHERE name=%s", user)
  162. return fullname and fullname[0][0] or ''
  163. def get_system_managers():
  164. """returns all system manager's profile details"""
  165. system_managers = webnotes.conn.sql("""select distinct name
  166. from tabProfile p
  167. where docstatus < 2 and enabled = 1
  168. and name not in ("Administrator", "Guest")
  169. and exists (select * from tabUserRole ur
  170. where ur.parent = p.name and ur.role="System Manager")""")
  171. return [p[0] for p in system_managers]