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.

profile.py 6.5 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.defaults = None
  31. self.name = name or webnotes.session.get('user')
  32. self.roles = []
  33. self.all_read = []
  34. self.can_create = []
  35. self.can_read = []
  36. self.can_write = []
  37. self.can_cancel = []
  38. self.can_search = []
  39. self.can_get_report = []
  40. self.allow_modules = []
  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`, `report`
  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', 'report'):
  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. if p.get('report'):
  103. self.can_get_report.append(dt)
  104. self.can_get_report += dtp['child_tables']
  105. if not dtp.get('istable'):
  106. if not dtp.get('issingle') and not dtp.get('read_only'):
  107. self.can_search.append(dt)
  108. if not dtp.get('module') in self.allow_modules:
  109. self.allow_modules.append(dtp.get('module'))
  110. self.can_write += self.can_create
  111. self.can_write += self.in_create
  112. self.can_read += self.can_write
  113. self.all_read += self.can_read
  114. def get_defaults(self):
  115. if not self.defaults:
  116. roles = self.get_roles() + [self.name]
  117. res = webnotes.conn.sql("""select defkey, defvalue
  118. from `tabDefaultValue` where parent in ("%s") order by idx""" % '", "'.join(roles))
  119. self.defaults = {'owner': [self.name], "user": [self.name]}
  120. for rec in res:
  121. if not self.defaults.has_key(rec[0]):
  122. self.defaults[rec[0]] = []
  123. self.defaults[rec[0]].append(rec[1] or '')
  124. return self.defaults
  125. # update recent documents
  126. def update_recent(self, dt, dn):
  127. rdl = webnotes.cache().get_value("recent:" + self.name) or []
  128. new_rd = [dt, dn]
  129. # clear if exists
  130. for i in range(len(rdl)):
  131. rd = rdl[i]
  132. if rd==new_rd:
  133. del rdl[i]
  134. break
  135. if len(rdl) > 19:
  136. rdl = rdl[:19]
  137. rdl = [new_rd] + rdl
  138. r = webnotes.cache().set_value("recent:" + self.name, rdl)
  139. def load_profile(self):
  140. d = webnotes.conn.sql("""select email, first_name, last_name,
  141. email_signature, theme, background_image
  142. from tabProfile where name = %s""", self.name, as_dict=1)[0]
  143. if not self.can_read:
  144. self.build_permissions()
  145. d.name = self.name
  146. d.recent = json.dumps(webnotes.cache().get_value("recent:" + self.name) or [])
  147. if not d.theme:
  148. d.theme = "Default"
  149. d['roles'] = self.get_roles()
  150. d['defaults'] = self.get_defaults()
  151. d['can_create'] = self.can_create
  152. d['can_write'] = self.can_write
  153. d['can_read'] = list(set(self.can_read))
  154. d['can_cancel'] = list(set(self.can_cancel))
  155. d['can_get_report'] = list(set(self.can_get_report))
  156. d['allow_modules'] = self.allow_modules
  157. d['all_read'] = self.all_read
  158. d['can_search'] = list(set(self.can_search))
  159. d['in_create'] = self.in_create
  160. return d
  161. def get_user_fullname(user):
  162. fullname = webnotes.conn.sql("SELECT CONCAT_WS(' ', first_name, last_name) FROM `tabProfile` WHERE name=%s", user)
  163. return fullname and fullname[0][0] or ''
  164. def get_system_managers():
  165. """returns all system manager's profile details"""
  166. system_managers = webnotes.conn.sql("""select distinct name
  167. from tabProfile p
  168. where docstatus < 2 and enabled = 1
  169. and name not in ("Administrator", "Guest")
  170. and exists (select * from tabUserRole ur
  171. where ur.parent = p.name and ur.role="System Manager")""")
  172. return [p[0] for p in system_managers]