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.
 
 
 
 
 
 

222 line
6.4 KiB

  1. import webnotes
  2. class Profile:
  3. """
  4. A profile object is created at the beginning of every request with details of the use.
  5. The global profile object is `webnotes.user`
  6. """
  7. def __init__(self, name=''):
  8. self.name = name or webnotes.session.get('user')
  9. self.roles = []
  10. self.can_create = []
  11. self.can_read = []
  12. self.can_write = []
  13. self.can_get_report = []
  14. def _load_roles(self):
  15. res = webnotes.conn.sql('select role from tabUserRole where parent = %s', self.name)
  16. self.roles = []
  17. for t in res:
  18. if t[0]: self.roles.append(t[0])
  19. if webnotes.session.get('user') == 'Guest':
  20. self.roles.append('Guest')
  21. else:
  22. self.roles.append('All')
  23. return self.roles
  24. def get_roles(self):
  25. """
  26. get list of roles
  27. """
  28. if self.roles:
  29. return self.roles
  30. return self._load_roles()
  31. def get_allow_list(self, key):
  32. """
  33. Internal - get list of DocType where `key` is allowed. Key is either 'read', 'write' or 'create'
  34. """
  35. conn = webnotes.conn
  36. roles = self.get_roles()
  37. return [r[0] for r in conn.sql('SELECT DISTINCT t1.parent FROM `tabDocPerm` t1, tabDocType t2 WHERE t1.`%s`=1 AND t1.parent not like "old_parent:%%" AND t1.parent = t2.name AND IFNULL(t2.istable,0) = 0 AND t1.role in ("%s") order by t1.parent' % (key, '", "'.join(roles)))]
  38. def get_create_list(self):
  39. """
  40. Get list of DocTypes the user can create. Will filter DocTypes tagged with 'not_in_create' and table
  41. """
  42. cl = self.get_allow_list('create')
  43. conn = webnotes.conn
  44. no_create_list = [r[0] for r in conn.sql('select name from tabDocType where ifnull(in_create,0)=1 or ifnull(istable,0)=1 or ifnull(issingle,0)=1')]
  45. self.can_create = filter(lambda x: x not in no_create_list, cl)
  46. return self.can_create
  47. def get_read_list(self):
  48. """
  49. Get list of DocTypes the user can read
  50. """
  51. self.can_read = list(set(self.get_allow_list('read') + self.get_allow_list('write')))
  52. return self.can_read
  53. def get_report_list(self):
  54. conn = webnotes.conn
  55. # get all tables list
  56. res = conn.sql('SELECT parent, options from tabDocField where fieldtype="Table"')
  57. table_types, all_tabletypes = {}, []
  58. # make a dictionary fo all table types
  59. for t in res:
  60. all_tabletypes.append(t[1])
  61. if not table_types.has_key(t[0]):
  62. table_types[t[0]] = []
  63. table_types[t[0]].append(t[1])
  64. no_search_list = [r[0] for r in conn.sql('SELECT name FROM tabDocType WHERE read_only = 1 ORDER BY name')]
  65. # make the lists
  66. for f in self.can_read:
  67. tl = table_types.get(f, None)
  68. if tl:
  69. for t in tl:
  70. if t and (not t in self.can_get_report) and (not t in no_search_list):
  71. self.can_get_report.append(t)
  72. if f and (not f in self.can_get_report) and (not f in no_search_list):
  73. self.can_get_report.append(f)
  74. return self.can_get_report
  75. def get_write_list(self):
  76. """
  77. Get list of DocTypes the user can write
  78. """
  79. self.can_write = self.get_allow_list('write')
  80. return self.can_write
  81. def get_home_page(self):
  82. """
  83. Get the name of the user's home page from the `Control Panel`
  84. """
  85. roles = self.get_roles()
  86. hpl = webnotes.conn.sql("select role, home_page from `tabDefault Home Page` where parent='Control Panel' order by idx asc")
  87. for h in hpl:
  88. if h[0] in roles:
  89. return h[1]
  90. return webnotes.conn.get_value('Control Panel',None,'home_page') or 'Login Page'
  91. def get_defaults(self):
  92. """
  93. Get the user's default values based on user and role profile
  94. """
  95. roles = self.get_roles() + [self.name]
  96. res = webnotes.conn.sql('select defkey, defvalue from `tabDefaultValue` where parent in ("%s")' % '", "'.join(roles))
  97. self.defaults = {'owner': [self.name,]}
  98. for rec in res:
  99. if not self.defaults.has_key(rec[0]):
  100. self.defaults[rec[0]] = []
  101. self.defaults[rec[0]].append(rec[1] or '')
  102. return self.defaults
  103. def get_hide_tips(self):
  104. try:
  105. return webnotes.conn.sql("select hide_tips from tabProfile where name=%s", self.name)[0][0] or 0
  106. except:
  107. return 0
  108. # update recent documents
  109. def update_recent(self, dt, dn):
  110. """
  111. Update the user's `Recent` list with the given `dt` and `dn`
  112. """
  113. conn = webnotes.conn
  114. from webnotes.utils import cstr
  115. import json
  116. # get list of child tables, so we know what not to add in the recent list
  117. child_tables = [t[0] for t in conn.sql('select name from tabDocType where istable = 1')]
  118. if not (dt in ['Print Format', 'Start Page', 'Event', 'ToDo Item', 'Search Criteria']) and not webnotes.is_testing and not (dt in child_tables):
  119. r = webnotes.conn.sql("select recent_documents from tabProfile where name=%s", self.name)[0][0] or ''
  120. # clear old style (to be removed)
  121. if '~~' in r: r = ''
  122. rdl = json.loads(r or '[]')
  123. new_rd = [dt, dn]
  124. # clear if exists
  125. for i in range(len(rdl)):
  126. rd = rdl[i]
  127. if rd==new_rd:
  128. del rdl[i]
  129. break
  130. rdl.append(new_rd)
  131. if len(rdl) > 20:
  132. rdl = rdl[:20]
  133. self.recent = json.dumps(rdl)
  134. webnotes.conn.sql("update tabProfile set recent_documents=%s where name=%s", (self.recent, self.name))
  135. def load_profile(self):
  136. """
  137. Return a dictionary of user properites to be stored in the session
  138. """
  139. t = webnotes.conn.sql('select email, first_name, last_name, recent_documents from tabProfile where name = %s', self.name)[0]
  140. d = {}
  141. d['name'] = self.name
  142. d['email'] = t[0] or ''
  143. d['first_name'] = t[1] or ''
  144. d['last_name'] = t[2] or ''
  145. d['recent'] = t[3] or ''
  146. d['hide_tips'] = self.get_hide_tips()
  147. d['roles'] = self.get_roles()
  148. d['defaults'] = self.get_defaults()
  149. d['can_create'] = self.get_create_list()
  150. d['can_read'] = self.get_read_list()
  151. d['can_write'] = self.get_write_list()
  152. d['can_get_report'] = self.get_report_list()
  153. return d
  154. def load_from_session(self, d):
  155. """
  156. Setup the user profile from the dictionary saved in the session (generated by `load_profile`)
  157. """
  158. self.can_create = d['can_create']
  159. self.can_read = d['can_read']
  160. self.can_write = d['can_write']
  161. self.can_get_report = d['can_get_report']
  162. self.roles = d['roles']
  163. self.defaults = d['defaults']
  164. def get_user_img():
  165. if not webnotes.form.getvalue('username'):
  166. webnotes.response['message'] = 'no_img_m'
  167. return
  168. f = webnotes.conn.sql("select file_list from tabProfile where name=%s", webnotes.form.getvalue('username',''))
  169. if f:
  170. if f[0][0]:
  171. lst = f[0][0].split('\n')
  172. webnotes.response['message'] = lst[0].split(',')[1]
  173. else:
  174. webnotes.response['message'] = 'no_img_m'
  175. else:
  176. webnotes.response['message'] = 'no_img_m'