res_users.py 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import binascii
  4. import contextlib
  5. import datetime
  6. import hmac
  7. import ipaddress
  8. import itertools
  9. import json
  10. import logging
  11. import os
  12. import time
  13. from collections import defaultdict
  14. from functools import wraps
  15. from hashlib import sha256
  16. from itertools import chain, repeat
  17. from markupsafe import Markup
  18. import babel.core
  19. import pytz
  20. from lxml import etree
  21. from lxml.builder import E
  22. from passlib.context import CryptContext
  23. from psycopg2 import sql
  24. from odoo import api, fields, models, tools, SUPERUSER_ID, _, Command
  25. from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
  26. from odoo.exceptions import AccessDenied, AccessError, UserError, ValidationError
  27. from odoo.http import request, DEFAULT_LANG
  28. from odoo.osv import expression
  29. from odoo.service.db import check_super
  30. from odoo.tools import is_html_empty, partition, collections, frozendict, lazy_property
  31. _logger = logging.getLogger(__name__)
  32. # Only users who can modify the user (incl. the user herself) see the real contents of these fields
  33. USER_PRIVATE_FIELDS = []
  34. MIN_ROUNDS = 350000
  35. concat = chain.from_iterable
  36. #
  37. # Functions for manipulating boolean and selection pseudo-fields
  38. #
  39. def name_boolean_group(id):
  40. return 'in_group_' + str(id)
  41. def name_selection_groups(ids):
  42. return 'sel_groups_' + '_'.join(str(it) for it in sorted(ids))
  43. def is_boolean_group(name):
  44. return name.startswith('in_group_')
  45. def is_selection_groups(name):
  46. return name.startswith('sel_groups_')
  47. def is_reified_group(name):
  48. return is_boolean_group(name) or is_selection_groups(name)
  49. def get_boolean_group(name):
  50. return int(name[9:])
  51. def get_selection_groups(name):
  52. return [int(v) for v in name[11:].split('_')]
  53. def parse_m2m(commands):
  54. "return a list of ids corresponding to a many2many value"
  55. ids = []
  56. for command in commands:
  57. if isinstance(command, (tuple, list)):
  58. if command[0] in (Command.UPDATE, Command.LINK):
  59. ids.append(command[1])
  60. elif command[0] == Command.CLEAR:
  61. ids = []
  62. elif command[0] == Command.SET:
  63. ids = list(command[2])
  64. else:
  65. ids.append(command)
  66. return ids
  67. def _jsonable(o):
  68. try: json.dumps(o)
  69. except TypeError: return False
  70. else: return True
  71. def check_identity(fn):
  72. """ Wrapped method should be an *action method* (called from a button
  73. type=object), and requires extra security to be executed. This decorator
  74. checks if the identity (password) has been checked in the last 10mn, and
  75. pops up an identity check wizard if not.
  76. Prevents access outside of interactive contexts (aka with a request)
  77. """
  78. @wraps(fn)
  79. def wrapped(self):
  80. if not request:
  81. raise UserError(_("This method can only be accessed over HTTP"))
  82. if request.session.get('identity-check-last', 0) > time.time() - 10 * 60:
  83. # update identity-check-last like github?
  84. return fn(self)
  85. w = self.sudo().env['res.users.identitycheck'].create({
  86. 'request': json.dumps([
  87. { # strip non-jsonable keys (e.g. mapped to recordsets like binary_field_real_user)
  88. k: v for k, v in self.env.context.items()
  89. if _jsonable(v)
  90. },
  91. self._name,
  92. self.ids,
  93. fn.__name__
  94. ])
  95. })
  96. return {
  97. 'type': 'ir.actions.act_window',
  98. 'res_model': 'res.users.identitycheck',
  99. 'res_id': w.id,
  100. 'name': _("Security Control"),
  101. 'target': 'new',
  102. 'views': [(False, 'form')],
  103. }
  104. wrapped.__has_check_identity = True
  105. return wrapped
  106. #----------------------------------------------------------
  107. # Basic res.groups and res.users
  108. #----------------------------------------------------------
  109. class Groups(models.Model):
  110. _name = "res.groups"
  111. _description = "Access Groups"
  112. _rec_name = 'full_name'
  113. _order = 'name'
  114. name = fields.Char(required=True, translate=True)
  115. users = fields.Many2many('res.users', 'res_groups_users_rel', 'gid', 'uid')
  116. model_access = fields.One2many('ir.model.access', 'group_id', string='Access Controls', copy=True)
  117. rule_groups = fields.Many2many('ir.rule', 'rule_group_rel',
  118. 'group_id', 'rule_group_id', string='Rules', domain="[('global', '=', False)]")
  119. menu_access = fields.Many2many('ir.ui.menu', 'ir_ui_menu_group_rel', 'gid', 'menu_id', string='Access Menu')
  120. view_access = fields.Many2many('ir.ui.view', 'ir_ui_view_group_rel', 'group_id', 'view_id', string='Views')
  121. comment = fields.Text(translate=True)
  122. category_id = fields.Many2one('ir.module.category', string='Application', index=True)
  123. color = fields.Integer(string='Color Index')
  124. full_name = fields.Char(compute='_compute_full_name', string='Group Name', search='_search_full_name')
  125. share = fields.Boolean(string='Share Group', help="Group created to set access rights for sharing data with some users.")
  126. _sql_constraints = [
  127. ('name_uniq', 'unique (category_id, name)', 'The name of the group must be unique within an application!')
  128. ]
  129. @api.constrains('users')
  130. def _check_one_user_type(self):
  131. self.users._check_one_user_type()
  132. @api.ondelete(at_uninstall=False)
  133. def _unlink_except_settings_group(self):
  134. classified = self.env['res.config.settings']._get_classified_fields()
  135. for _name, _groups, implied_group in classified['group']:
  136. if implied_group.id in self.ids:
  137. raise ValidationError(_('You cannot delete a group linked with a settings field.'))
  138. @api.depends('category_id.name', 'name')
  139. def _compute_full_name(self):
  140. # Important: value must be stored in environment of group, not group1!
  141. for group, group1 in zip(self, self.sudo()):
  142. if group1.category_id:
  143. group.full_name = '%s / %s' % (group1.category_id.name, group1.name)
  144. else:
  145. group.full_name = group1.name
  146. def _search_full_name(self, operator, operand):
  147. lst = True
  148. if isinstance(operand, bool):
  149. return [[('name', operator, operand)]]
  150. if isinstance(operand, str):
  151. lst = False
  152. operand = [operand]
  153. where = []
  154. for group in operand:
  155. values = [v for v in group.split('/') if v]
  156. group_name = values.pop().strip()
  157. category_name = values and '/'.join(values).strip() or group_name
  158. group_domain = [('name', operator, lst and [group_name] or group_name)]
  159. category_ids = self.env['ir.module.category'].sudo()._search(
  160. [('name', operator, [category_name] if lst else category_name)])
  161. category_domain = [('category_id', 'in', category_ids)]
  162. if operator in expression.NEGATIVE_TERM_OPERATORS and not values:
  163. category_domain = expression.OR([category_domain, [('category_id', '=', False)]])
  164. if (operator in expression.NEGATIVE_TERM_OPERATORS) == (not values):
  165. sub_where = expression.AND([group_domain, category_domain])
  166. else:
  167. sub_where = expression.OR([group_domain, category_domain])
  168. if operator in expression.NEGATIVE_TERM_OPERATORS:
  169. where = expression.AND([where, sub_where])
  170. else:
  171. where = expression.OR([where, sub_where])
  172. return where
  173. @api.model
  174. def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None):
  175. # add explicit ordering if search is sorted on full_name
  176. if order and order.startswith('full_name'):
  177. groups = super(Groups, self).search(args)
  178. groups = groups.sorted('full_name', reverse=order.endswith('DESC'))
  179. groups = groups[offset:offset+limit] if limit else groups[offset:]
  180. return len(groups) if count else groups.ids
  181. return super(Groups, self)._search(args, offset=offset, limit=limit, order=order, count=count, access_rights_uid=access_rights_uid)
  182. def copy(self, default=None):
  183. self.ensure_one()
  184. chosen_name = default.get('name') if default else ''
  185. default_name = chosen_name or _('%s (copy)', self.name)
  186. default = dict(default or {}, name=default_name)
  187. return super(Groups, self).copy(default)
  188. def write(self, vals):
  189. if 'name' in vals:
  190. if vals['name'].startswith('-'):
  191. raise UserError(_('The name of the group can not start with "-"'))
  192. # invalidate caches before updating groups, since the recomputation of
  193. # field 'share' depends on method has_group()
  194. # DLE P139
  195. if self.ids:
  196. self.env['ir.model.access'].call_cache_clearing_methods()
  197. return super(Groups, self).write(vals)
  198. def _ensure_xml_id(self):
  199. """Return the groups external identifiers, creating the external identifier for groups missing one"""
  200. result = self.get_external_id()
  201. missings = {group_id: f'__custom__.group_{group_id}' for group_id, ext_id in result.items() if not ext_id}
  202. if missings:
  203. self.env['ir.model.data'].create(
  204. [
  205. {
  206. 'name': name.split('.')[1],
  207. 'model': 'res.groups',
  208. 'res_id': group_id,
  209. 'module': name.split('.')[0],
  210. }
  211. for group_id, name in missings.items()
  212. ]
  213. )
  214. result.update(missings)
  215. return result
  216. class ResUsersLog(models.Model):
  217. _name = 'res.users.log'
  218. _order = 'id desc'
  219. _description = 'Users Log'
  220. # Currenly only uses the magical fields: create_uid, create_date,
  221. # for recording logins. To be extended for other uses (chat presence, etc.)
  222. @api.autovacuum
  223. def _gc_user_logs(self):
  224. self._cr.execute("""
  225. DELETE FROM res_users_log log1 WHERE EXISTS (
  226. SELECT 1 FROM res_users_log log2
  227. WHERE log1.create_uid = log2.create_uid
  228. AND log1.create_date < log2.create_date
  229. )
  230. """)
  231. _logger.info("GC'd %d user log entries", self._cr.rowcount)
  232. class Users(models.Model):
  233. """ User class. A res.users record models an OpenERP user and is different
  234. from an employee.
  235. res.users class now inherits from res.partner. The partner model is
  236. used to store the data related to the partner: lang, name, address,
  237. avatar, ... The user model is now dedicated to technical data.
  238. """
  239. _name = "res.users"
  240. _description = 'User'
  241. _inherits = {'res.partner': 'partner_id'}
  242. _order = 'name, login'
  243. @property
  244. def SELF_READABLE_FIELDS(self):
  245. """ The list of fields a user can read on their own user record.
  246. In order to add fields, please override this property on model extensions.
  247. """
  248. return [
  249. 'signature', 'company_id', 'login', 'email', 'name', 'image_1920',
  250. 'image_1024', 'image_512', 'image_256', 'image_128', 'lang', 'tz',
  251. 'tz_offset', 'groups_id', 'partner_id', '__last_update', 'action_id',
  252. 'avatar_1920', 'avatar_1024', 'avatar_512', 'avatar_256', 'avatar_128',
  253. 'share',
  254. ]
  255. @property
  256. def SELF_WRITEABLE_FIELDS(self):
  257. """ The list of fields a user can write on their own user record.
  258. In order to add fields, please override this property on model extensions.
  259. """
  260. return ['signature', 'action_id', 'company_id', 'email', 'name', 'image_1920', 'lang', 'tz']
  261. def _default_groups(self):
  262. default_user_id = self.env['ir.model.data']._xmlid_to_res_id('base.default_user', raise_if_not_found=False)
  263. return self.env['res.users'].browse(default_user_id).sudo().groups_id if default_user_id else []
  264. partner_id = fields.Many2one('res.partner', required=True, ondelete='restrict', auto_join=True, index=True,
  265. string='Related Partner', help='Partner-related data of the user')
  266. login = fields.Char(required=True, help="Used to log into the system")
  267. password = fields.Char(
  268. compute='_compute_password', inverse='_set_password',
  269. invisible=True, copy=False,
  270. help="Keep empty if you don't want the user to be able to connect on the system.")
  271. new_password = fields.Char(string='Set Password',
  272. compute='_compute_password', inverse='_set_new_password',
  273. help="Specify a value only when creating a user or if you're "\
  274. "changing the user's password, otherwise leave empty. After "\
  275. "a change of password, the user has to login again.")
  276. signature = fields.Html(string="Email Signature", compute='_compute_signature', readonly=False, store=True)
  277. active = fields.Boolean(default=True)
  278. active_partner = fields.Boolean(related='partner_id.active', readonly=True, string="Partner is Active")
  279. action_id = fields.Many2one('ir.actions.actions', string='Home Action',
  280. help="If specified, this action will be opened at log on for this user, in addition to the standard menu.")
  281. groups_id = fields.Many2many('res.groups', 'res_groups_users_rel', 'uid', 'gid', string='Groups', default=_default_groups)
  282. log_ids = fields.One2many('res.users.log', 'create_uid', string='User log entries')
  283. login_date = fields.Datetime(related='log_ids.create_date', string='Latest authentication', readonly=False)
  284. share = fields.Boolean(compute='_compute_share', compute_sudo=True, string='Share User', store=True,
  285. help="External user with limited access, created only for the purpose of sharing data.")
  286. companies_count = fields.Integer(compute='_compute_companies_count', string="Number of Companies")
  287. tz_offset = fields.Char(compute='_compute_tz_offset', string='Timezone offset', invisible=True)
  288. # Special behavior for this field: res.company.search() will only return the companies
  289. # available to the current user (should be the user's companies?), when the user_preference
  290. # context is set.
  291. company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env.company.id,
  292. help='The default company for this user.', context={'user_preference': True})
  293. company_ids = fields.Many2many('res.company', 'res_company_users_rel', 'user_id', 'cid',
  294. string='Companies', default=lambda self: self.env.company.ids)
  295. # overridden inherited fields to bypass access rights, in case you have
  296. # access to the user but not its corresponding partner
  297. name = fields.Char(related='partner_id.name', inherited=True, readonly=False)
  298. email = fields.Char(related='partner_id.email', inherited=True, readonly=False)
  299. accesses_count = fields.Integer('# Access Rights', help='Number of access rights that apply to the current user',
  300. compute='_compute_accesses_count', compute_sudo=True)
  301. rules_count = fields.Integer('# Record Rules', help='Number of record rules that apply to the current user',
  302. compute='_compute_accesses_count', compute_sudo=True)
  303. groups_count = fields.Integer('# Groups', help='Number of groups that apply to the current user',
  304. compute='_compute_accesses_count', compute_sudo=True)
  305. _sql_constraints = [
  306. ('login_key', 'UNIQUE (login)', 'You can not have two users with the same login !')
  307. ]
  308. def init(self):
  309. cr = self.env.cr
  310. # allow setting plaintext passwords via SQL and have them
  311. # automatically encrypted at startup: look for passwords which don't
  312. # match the "extended" MCF and pass those through passlib.
  313. # Alternative: iterate on *all* passwords and use CryptContext.identify
  314. cr.execute("""
  315. SELECT id, password FROM res_users
  316. WHERE password IS NOT NULL
  317. AND password !~ '^\$[^$]+\$[^$]+\$.'
  318. """)
  319. if self.env.cr.rowcount:
  320. Users = self.sudo()
  321. for uid, pw in cr.fetchall():
  322. Users.browse(uid).password = pw
  323. def _set_password(self):
  324. ctx = self._crypt_context()
  325. for user in self:
  326. self._set_encrypted_password(user.id, ctx.hash(user.password))
  327. def _set_encrypted_password(self, uid, pw):
  328. assert self._crypt_context().identify(pw) != 'plaintext'
  329. self.env.cr.execute(
  330. 'UPDATE res_users SET password=%s WHERE id=%s',
  331. (pw, uid)
  332. )
  333. self.browse(uid).invalidate_recordset(['password'])
  334. def _check_credentials(self, password, env):
  335. """ Validates the current user's password.
  336. Override this method to plug additional authentication methods.
  337. Overrides should:
  338. * call `super` to delegate to parents for credentials-checking
  339. * catch AccessDenied and perform their own checking
  340. * (re)raise AccessDenied if the credentials are still invalid
  341. according to their own validation method
  342. When trying to check for credentials validity, call _check_credentials
  343. instead.
  344. """
  345. """ Override this method to plug additional authentication methods"""
  346. assert password
  347. self.env.cr.execute(
  348. "SELECT COALESCE(password, '') FROM res_users WHERE id=%s",
  349. [self.env.user.id]
  350. )
  351. [hashed] = self.env.cr.fetchone()
  352. valid, replacement = self._crypt_context()\
  353. .verify_and_update(password, hashed)
  354. if replacement is not None:
  355. self._set_encrypted_password(self.env.user.id, replacement)
  356. if not valid:
  357. raise AccessDenied()
  358. def _compute_password(self):
  359. for user in self:
  360. user.password = ''
  361. user.new_password = ''
  362. def _set_new_password(self):
  363. for user in self:
  364. if not user.new_password:
  365. # Do not update the password if no value is provided, ignore silently.
  366. # For example web client submits False values for all empty fields.
  367. continue
  368. if user == self.env.user:
  369. # To change their own password, users must use the client-specific change password wizard,
  370. # so that the new password is immediately used for further RPC requests, otherwise the user
  371. # will face unexpected 'Access Denied' exceptions.
  372. raise UserError(_('Please use the change password wizard (in User Preferences or User menu) to change your own password.'))
  373. else:
  374. user.password = user.new_password
  375. @api.depends('name')
  376. def _compute_signature(self):
  377. for user in self.filtered(lambda user: user.name and is_html_empty(user.signature)):
  378. user.signature = Markup('<p>--<br />%s</p>') % user['name']
  379. @api.depends('groups_id')
  380. def _compute_share(self):
  381. user_group_id = self.env['ir.model.data']._xmlid_to_res_id('base.group_user')
  382. internal_users = self.filtered_domain([('groups_id', 'in', [user_group_id])])
  383. internal_users.share = False
  384. (self - internal_users).share = True
  385. @api.depends('company_id')
  386. def _compute_companies_count(self):
  387. self.companies_count = self.env['res.company'].sudo().search_count([])
  388. @api.depends('tz')
  389. def _compute_tz_offset(self):
  390. for user in self:
  391. user.tz_offset = datetime.datetime.now(pytz.timezone(user.tz or 'GMT')).strftime('%z')
  392. @api.depends('groups_id')
  393. def _compute_accesses_count(self):
  394. for user in self:
  395. groups = user.groups_id
  396. user.accesses_count = len(groups.model_access)
  397. user.rules_count = len(groups.rule_groups)
  398. user.groups_count = len(groups)
  399. @api.onchange('login')
  400. def on_change_login(self):
  401. if self.login and tools.single_email_re.match(self.login):
  402. self.email = self.login
  403. @api.onchange('parent_id')
  404. def onchange_parent_id(self):
  405. return self.partner_id.onchange_parent_id()
  406. def _read(self, fields):
  407. super(Users, self)._read(fields)
  408. if set(USER_PRIVATE_FIELDS).intersection(fields):
  409. if self.check_access_rights('write', raise_exception=False):
  410. return
  411. for record in self:
  412. for f in USER_PRIVATE_FIELDS:
  413. try:
  414. record._cache[f]
  415. record._cache[f] = '********'
  416. except Exception:
  417. # skip SpecialValue (e.g. for missing record or access right)
  418. pass
  419. @api.constrains('company_id', 'company_ids', 'active')
  420. def _check_company(self):
  421. for user in self.filtered(lambda u: u.active):
  422. if user.company_id not in user.company_ids:
  423. raise ValidationError(
  424. _('Company %(company_name)s is not in the allowed companies for user %(user_name)s (%(company_allowed)s).',
  425. company_name=user.company_id.name,
  426. user_name=user.name,
  427. company_allowed=', '.join(user.mapped('company_ids.name')))
  428. )
  429. @api.constrains('action_id')
  430. def _check_action_id(self):
  431. action_open_website = self.env.ref('base.action_open_website', raise_if_not_found=False)
  432. if action_open_website and any(user.action_id.id == action_open_website.id for user in self):
  433. raise ValidationError(_('The "App Switcher" action cannot be selected as home action.'))
  434. # Prevent using reload actions.
  435. # We use sudo() because "Access rights" admins can't read action models
  436. for user in self.sudo():
  437. if user.action_id.type == "ir.actions.client":
  438. action = self.env["ir.actions.client"].browse(user.action_id.id) # magic
  439. if action.tag == "reload":
  440. raise ValidationError(_('The "%s" action cannot be selected as home action.', action.name))
  441. @api.constrains('groups_id')
  442. def _check_one_user_type(self):
  443. """We check that no users are both portal and users (same with public).
  444. This could typically happen because of implied groups.
  445. """
  446. user_types_category = self.env.ref('base.module_category_user_type', raise_if_not_found=False)
  447. user_types_groups = self.env['res.groups'].search(
  448. [('category_id', '=', user_types_category.id)]) if user_types_category else False
  449. if user_types_groups: # needed at install
  450. if self._has_multiple_groups(user_types_groups.ids):
  451. raise ValidationError(_('The user cannot have more than one user types.'))
  452. def _has_multiple_groups(self, group_ids):
  453. """The method is not fast if the list of ids is very long;
  454. so we rather check all users than limit to the size of the group
  455. :param group_ids: list of group ids
  456. :return: boolean: is there at least a user in at least 2 of the provided groups
  457. """
  458. if group_ids:
  459. args = [tuple(group_ids)]
  460. if len(self.ids) == 1:
  461. where_clause = "AND r.uid = %s"
  462. args.append(self.id)
  463. else:
  464. where_clause = "" # default; we check ALL users (actually pretty efficient)
  465. query = """
  466. SELECT 1 FROM res_groups_users_rel WHERE EXISTS(
  467. SELECT r.uid
  468. FROM res_groups_users_rel r
  469. WHERE r.gid IN %s""" + where_clause + """
  470. GROUP BY r.uid HAVING COUNT(r.gid) > 1
  471. )
  472. """
  473. self.env.cr.execute(query, args)
  474. return bool(self.env.cr.fetchall())
  475. else:
  476. return False
  477. def toggle_active(self):
  478. for user in self:
  479. if not user.active and not user.partner_id.active:
  480. user.partner_id.toggle_active()
  481. super(Users, self).toggle_active()
  482. def read(self, fields=None, load='_classic_read'):
  483. if fields and self == self.env.user:
  484. readable = self.SELF_READABLE_FIELDS
  485. for key in fields:
  486. if not (key in readable or key.startswith('context_')):
  487. break
  488. else:
  489. # safe fields only, so we read as super-user to bypass access rights
  490. self = self.sudo()
  491. return super(Users, self).read(fields=fields, load=load)
  492. @api.model
  493. def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
  494. groupby_fields = set([groupby] if isinstance(groupby, str) else groupby)
  495. if groupby_fields.intersection(USER_PRIVATE_FIELDS):
  496. raise AccessError(_("Invalid 'group by' parameter"))
  497. return super(Users, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)
  498. @api.model
  499. def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None):
  500. if not self.env.su and args:
  501. domain_fields = {term[0] for term in args if isinstance(term, (tuple, list))}
  502. if domain_fields.intersection(USER_PRIVATE_FIELDS):
  503. raise AccessError(_('Invalid search criterion'))
  504. return super(Users, self)._search(args, offset=offset, limit=limit, order=order, count=count,
  505. access_rights_uid=access_rights_uid)
  506. @api.model_create_multi
  507. def create(self, vals_list):
  508. users = super(Users, self).create(vals_list)
  509. for user in users:
  510. # if partner is global we keep it that way
  511. if user.partner_id.company_id:
  512. user.partner_id.company_id = user.company_id
  513. user.partner_id.active = user.active
  514. return users
  515. def write(self, values):
  516. if values.get('active') and SUPERUSER_ID in self._ids:
  517. raise UserError(_("You cannot activate the superuser."))
  518. if values.get('active') == False and self._uid in self._ids:
  519. raise UserError(_("You cannot deactivate the user you're currently logged in as."))
  520. if values.get('active'):
  521. for user in self:
  522. if not user.active and not user.partner_id.active:
  523. user.partner_id.toggle_active()
  524. if self == self.env.user:
  525. writeable = self.SELF_WRITEABLE_FIELDS
  526. for key in list(values):
  527. if not (key in writeable or key.startswith('context_')):
  528. break
  529. else:
  530. if 'company_id' in values:
  531. if values['company_id'] not in self.env.user.company_ids.ids:
  532. del values['company_id']
  533. # safe fields only, so we write as super-user to bypass access rights
  534. self = self.sudo().with_context(binary_field_real_user=self.env.user)
  535. if 'groups_id' in values:
  536. default_user = self.env.ref('base.default_user', raise_if_not_found=False)
  537. if default_user and default_user in self:
  538. old_groups = default_user.groups_id
  539. res = super(Users, self).write(values)
  540. if 'groups_id' in values and default_user and default_user in self:
  541. # Sync added groups on default user template to existing users
  542. added_groups = default_user.groups_id - old_groups
  543. if added_groups:
  544. internal_users = self.env.ref('base.group_user').users - default_user
  545. internal_users.write({'groups_id': [Command.link(gid) for gid in added_groups.ids]})
  546. if 'company_id' in values:
  547. for user in self:
  548. # if partner is global we keep it that way
  549. if user.partner_id.company_id and user.partner_id.company_id.id != values['company_id']:
  550. user.partner_id.write({'company_id': user.company_id.id})
  551. if 'company_id' in values or 'company_ids' in values:
  552. # Reset lazy properties `company` & `companies` on all envs
  553. # This is unlikely in a business code to change the company of a user and then do business stuff
  554. # but in case it happens this is handled.
  555. # e.g. `account_test_savepoint.py` `setup_company_data`, triggered by `test_account_invoice_report.py`
  556. for env in list(self.env.transaction.envs):
  557. if env.user in self:
  558. lazy_property.reset_all(env)
  559. # clear caches linked to the users
  560. if self.ids and 'groups_id' in values:
  561. # DLE P139: Calling invalidate_cache on a new, well you lost everything as you wont be able to take it back from the cache
  562. # `test_00_equipment_multicompany_user`
  563. self.env['ir.model.access'].call_cache_clearing_methods()
  564. # per-method / per-model caches have been removed so the various
  565. # clear_cache/clear_caches methods pretty much just end up calling
  566. # Registry._clear_cache
  567. invalidation_fields = self._get_invalidation_fields()
  568. if (invalidation_fields & values.keys()) or any(key.startswith('context_') for key in values):
  569. self.clear_caches()
  570. return res
  571. @api.ondelete(at_uninstall=True)
  572. def _unlink_except_master_data(self):
  573. portal_user_template = self.env.ref('base.template_portal_user_id', False)
  574. default_user_template = self.env.ref('base.default_user', False)
  575. if SUPERUSER_ID in self.ids:
  576. raise UserError(_('You can not remove the admin user as it is used internally for resources created by Odoo (updates, module installation, ...)'))
  577. self.clear_caches()
  578. if (portal_user_template and portal_user_template in self) or (default_user_template and default_user_template in self):
  579. raise UserError(_('Deleting the template users is not allowed. Deleting this profile will compromise critical functionalities.'))
  580. @api.model
  581. def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
  582. args = args or []
  583. user_ids = []
  584. if operator not in expression.NEGATIVE_TERM_OPERATORS:
  585. if operator == 'ilike' and not (name or '').strip():
  586. domain = []
  587. else:
  588. domain = [('login', '=', name)]
  589. user_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
  590. if not user_ids:
  591. user_ids = self._search(expression.AND([[('name', operator, name)], args]), limit=limit, access_rights_uid=name_get_uid)
  592. return user_ids
  593. def copy(self, default=None):
  594. self.ensure_one()
  595. default = dict(default or {})
  596. if ('name' not in default) and ('partner_id' not in default):
  597. default['name'] = _("%s (copy)", self.name)
  598. if 'login' not in default:
  599. default['login'] = _("%s (copy)", self.login)
  600. return super(Users, self).copy(default)
  601. @api.model
  602. @tools.ormcache('self._uid')
  603. def context_get(self):
  604. user = self.env.user
  605. # determine field names to read
  606. name_to_key = {
  607. name: name[8:] if name.startswith('context_') else name
  608. for name in self._fields
  609. if name.startswith('context_') or name in ('lang', 'tz')
  610. }
  611. # use read() to not read other fields: this must work while modifying
  612. # the schema of models res.users or res.partner
  613. values = user.read(list(name_to_key), load=False)[0]
  614. context = {
  615. key: values[name]
  616. for name, key in name_to_key.items()
  617. }
  618. # ensure lang is set and available
  619. # context > request > company > english > any lang installed
  620. langs = [code for code, _ in self.env['res.lang'].get_installed()]
  621. lang = context.get('lang')
  622. if lang not in langs:
  623. lang = request.best_lang if request else None
  624. if lang not in langs:
  625. lang = self.env.user.company_id.partner_id.lang
  626. if lang not in langs:
  627. lang = DEFAULT_LANG
  628. if lang not in langs:
  629. lang = langs[0] if langs else DEFAULT_LANG
  630. context['lang'] = lang
  631. # ensure uid is set
  632. context['uid'] = self.env.uid
  633. return frozendict(context)
  634. @tools.ormcache('self.id')
  635. def _get_company_ids(self):
  636. # use search() instead of `self.company_ids` to avoid extra query for `active_test`
  637. domain = [('active', '=', True), ('user_ids', 'in', self.id)]
  638. return self.env['res.company'].search(domain)._ids
  639. @api.model
  640. def action_get(self):
  641. return self.sudo().env.ref('base.action_res_users_my').read()[0]
  642. def check_super(self, passwd):
  643. return check_super(passwd)
  644. @api.model
  645. def _get_invalidation_fields(self):
  646. return {
  647. 'groups_id', 'active', 'lang', 'tz', 'company_id', 'company_ids',
  648. *USER_PRIVATE_FIELDS,
  649. *self._get_session_token_fields()
  650. }
  651. @api.model
  652. def _update_last_login(self):
  653. # only create new records to avoid any side-effect on concurrent transactions
  654. # extra records will be deleted by the periodical garbage collection
  655. self.env['res.users.log'].create({}) # populated by defaults
  656. @api.model
  657. def _get_login_domain(self, login):
  658. return [('login', '=', login)]
  659. @api.model
  660. def _get_login_order(self):
  661. return self._order
  662. @classmethod
  663. def _login(cls, db, login, password, user_agent_env):
  664. if not password:
  665. raise AccessDenied()
  666. ip = request.httprequest.environ['REMOTE_ADDR'] if request else 'n/a'
  667. try:
  668. with cls.pool.cursor() as cr:
  669. self = api.Environment(cr, SUPERUSER_ID, {})[cls._name]
  670. with self._assert_can_auth(user=login):
  671. user = self.search(self._get_login_domain(login), order=self._get_login_order(), limit=1)
  672. if not user:
  673. raise AccessDenied()
  674. user = user.with_user(user)
  675. user._check_credentials(password, user_agent_env)
  676. tz = request.httprequest.cookies.get('tz') if request else None
  677. if tz in pytz.all_timezones and (not user.tz or not user.login_date):
  678. # first login or missing tz -> set tz to browser tz
  679. user.tz = tz
  680. user._update_last_login()
  681. except AccessDenied:
  682. _logger.info("Login failed for db:%s login:%s from %s", db, login, ip)
  683. raise
  684. _logger.info("Login successful for db:%s login:%s from %s", db, login, ip)
  685. return user.id
  686. @classmethod
  687. def authenticate(cls, db, login, password, user_agent_env):
  688. """Verifies and returns the user ID corresponding to the given
  689. ``login`` and ``password`` combination, or False if there was
  690. no matching user.
  691. :param str db: the database on which user is trying to authenticate
  692. :param str login: username
  693. :param str password: user password
  694. :param dict user_agent_env: environment dictionary describing any
  695. relevant environment attributes
  696. """
  697. uid = cls._login(db, login, password, user_agent_env=user_agent_env)
  698. if user_agent_env and user_agent_env.get('base_location'):
  699. with cls.pool.cursor() as cr:
  700. env = api.Environment(cr, uid, {})
  701. if env.user.has_group('base.group_system'):
  702. # Successfully logged in as system user!
  703. # Attempt to guess the web base url...
  704. try:
  705. base = user_agent_env['base_location']
  706. ICP = env['ir.config_parameter']
  707. if not ICP.get_param('web.base.url.freeze'):
  708. ICP.set_param('web.base.url', base)
  709. except Exception:
  710. _logger.exception("Failed to update web.base.url configuration parameter")
  711. return uid
  712. @classmethod
  713. @tools.ormcache('uid', 'passwd')
  714. def check(cls, db, uid, passwd):
  715. """Verifies that the given (uid, password) is authorized for the database ``db`` and
  716. raise an exception if it is not."""
  717. if not passwd:
  718. # empty passwords disallowed for obvious security reasons
  719. raise AccessDenied()
  720. with contextlib.closing(cls.pool.cursor()) as cr:
  721. self = api.Environment(cr, uid, {})[cls._name]
  722. with self._assert_can_auth(user=uid):
  723. if not self.env.user.active:
  724. raise AccessDenied()
  725. self._check_credentials(passwd, {'interactive': False})
  726. def _get_session_token_fields(self):
  727. return {'id', 'login', 'password', 'active'}
  728. @tools.ormcache('sid')
  729. def _compute_session_token(self, sid):
  730. """ Compute a session token given a session id and a user id """
  731. # retrieve the fields used to generate the session token
  732. session_fields = ', '.join(sorted(self._get_session_token_fields()))
  733. self.env.cr.execute("""SELECT %s, (SELECT value FROM ir_config_parameter WHERE key='database.secret')
  734. FROM res_users
  735. WHERE id=%%s""" % (session_fields), (self.id,))
  736. if self.env.cr.rowcount != 1:
  737. self.clear_caches()
  738. return False
  739. data_fields = self.env.cr.fetchone()
  740. # generate hmac key
  741. key = (u'%s' % (data_fields,)).encode('utf-8')
  742. # hmac the session id
  743. data = sid.encode('utf-8')
  744. h = hmac.new(key, data, sha256)
  745. # keep in the cache the token
  746. return h.hexdigest()
  747. @api.model
  748. def change_password(self, old_passwd, new_passwd):
  749. """Change current user password. Old password must be provided explicitly
  750. to prevent hijacking an existing user session, or for cases where the cleartext
  751. password is not used to authenticate requests.
  752. :return: True
  753. :raise: odoo.exceptions.AccessDenied when old password is wrong
  754. :raise: odoo.exceptions.UserError when new password is not set or empty
  755. """
  756. if not old_passwd:
  757. raise AccessDenied()
  758. # alternatively: use identitycheck wizard?
  759. self._check_credentials(old_passwd, {'interactive': True})
  760. # use self.env.user here, because it has uid=SUPERUSER_ID
  761. self.env.user._change_password(new_passwd)
  762. return True
  763. def _change_password(self, new_passwd):
  764. new_passwd = new_passwd.strip()
  765. if not new_passwd:
  766. raise UserError(_("Setting empty passwords is not allowed for security reasons!"))
  767. ip = request.httprequest.environ['REMOTE_ADDR'] if request else 'n/a'
  768. _logger.info(
  769. "Password change for %r (#%d) by %r (#%d) from %s",
  770. self.login, self.id,
  771. self.env.user.login, self.env.user.id,
  772. ip
  773. )
  774. self.password = new_passwd
  775. def _deactivate_portal_user(self, **post):
  776. """Try to remove the current portal user.
  777. This is used to give the opportunity to portal users to de-activate their accounts.
  778. Indeed, as the portal users can easily create accounts, they will sometimes wish
  779. it removed because they don't use this Odoo portal anymore.
  780. Before this feature, they would have to contact the website or the support to get
  781. their account removed, which could be tedious.
  782. """
  783. non_portal_users = self.filtered(lambda user: not user.share)
  784. if non_portal_users:
  785. raise AccessDenied(_(
  786. 'Only the portal users can delete their accounts. '
  787. 'The user(s) %s can not be deleted.',
  788. ', '.join(non_portal_users.mapped('name')),
  789. ))
  790. ip = request.httprequest.environ['REMOTE_ADDR'] if request else 'n/a'
  791. res_users_deletion_values = []
  792. for user in self:
  793. _logger.info(
  794. 'Account deletion asked for "%s" (#%i) from %s. '
  795. 'Archive the user and remove login information.',
  796. user.login, user.id, ip,
  797. )
  798. user.write({
  799. 'login': f'__deleted_user_{user.id}_{time.time()}',
  800. 'password': '',
  801. 'api_key_ids': Command.clear(),
  802. })
  803. res_users_deletion_values.append({
  804. 'user_id': user.id,
  805. 'state': 'todo',
  806. })
  807. # Here we try to archive the user / partner, and then add the user in a deletion
  808. # queue, to remove it from the database. As the deletion might fail (if the
  809. # partner is related to an invoice e.g.) it's important to archive it here.
  810. try:
  811. # A user can not self-deactivate
  812. self.with_user(SUPERUSER_ID).action_archive()
  813. except Exception:
  814. pass
  815. try:
  816. self.partner_id.action_archive()
  817. except Exception:
  818. pass
  819. # Add users in the deletion queue
  820. self.env['res.users.deletion'].create(res_users_deletion_values)
  821. def preference_save(self):
  822. return {
  823. 'type': 'ir.actions.client',
  824. 'tag': 'reload_context',
  825. }
  826. @check_identity
  827. def preference_change_password(self):
  828. return {
  829. 'type': 'ir.actions.act_window',
  830. 'target': 'new',
  831. 'res_model': 'change.password.own',
  832. 'view_mode': 'form',
  833. }
  834. @api.model
  835. def has_group(self, group_ext_id):
  836. # use singleton's id if called on a non-empty recordset, otherwise
  837. # context uid
  838. uid = self.id
  839. if uid and uid != self._uid:
  840. self = self.with_user(uid)
  841. return self._has_group(group_ext_id)
  842. @api.model
  843. @tools.ormcache('self._uid', 'group_ext_id')
  844. def _has_group(self, group_ext_id):
  845. """Checks whether user belongs to given group.
  846. :param str group_ext_id: external ID (XML ID) of the group.
  847. Must be provided in fully-qualified form (``module.ext_id``), as there
  848. is no implicit module to use..
  849. :return: True if the current user is a member of the group with the
  850. given external ID (XML ID), else False.
  851. """
  852. assert group_ext_id and '.' in group_ext_id, "External ID '%s' must be fully qualified" % group_ext_id
  853. module, ext_id = group_ext_id.split('.')
  854. self._cr.execute("""SELECT 1 FROM res_groups_users_rel WHERE uid=%s AND gid IN
  855. (SELECT res_id FROM ir_model_data WHERE module=%s AND name=%s AND model='res.groups')""",
  856. (self._uid, module, ext_id))
  857. return bool(self._cr.fetchone())
  858. def _action_show(self):
  859. """If self is a singleton, directly access the form view. If it is a recordset, open a tree view"""
  860. view_id = self.env.ref('base.view_users_form').id
  861. action = {
  862. 'type': 'ir.actions.act_window',
  863. 'res_model': 'res.users',
  864. 'context': {'create': False},
  865. }
  866. if len(self) > 1:
  867. action.update({
  868. 'name': _('Users'),
  869. 'view_mode': 'list,form',
  870. 'views': [[None, 'list'], [view_id, 'form']],
  871. 'domain': [('id', 'in', self.ids)],
  872. })
  873. else:
  874. action.update({
  875. 'view_mode': 'form',
  876. 'views': [[view_id, 'form']],
  877. 'res_id': self.id,
  878. })
  879. return action
  880. def action_show_groups(self):
  881. self.ensure_one()
  882. return {
  883. 'name': _('Groups'),
  884. 'view_mode': 'tree,form',
  885. 'res_model': 'res.groups',
  886. 'type': 'ir.actions.act_window',
  887. 'context': {'create': False, 'delete': False},
  888. 'domain': [('id','in', self.groups_id.ids)],
  889. 'target': 'current',
  890. }
  891. def action_show_accesses(self):
  892. self.ensure_one()
  893. return {
  894. 'name': _('Access Rights'),
  895. 'view_mode': 'tree,form',
  896. 'res_model': 'ir.model.access',
  897. 'type': 'ir.actions.act_window',
  898. 'context': {'create': False, 'delete': False},
  899. 'domain': [('id', 'in', self.groups_id.model_access.ids)],
  900. 'target': 'current',
  901. }
  902. def action_show_rules(self):
  903. self.ensure_one()
  904. return {
  905. 'name': _('Record Rules'),
  906. 'view_mode': 'tree,form',
  907. 'res_model': 'ir.rule',
  908. 'type': 'ir.actions.act_window',
  909. 'context': {'create': False, 'delete': False},
  910. 'domain': [('id', 'in', self.groups_id.rule_groups.ids)],
  911. 'target': 'current',
  912. }
  913. def _is_internal(self):
  914. self.ensure_one()
  915. return not self.sudo().share
  916. def _is_public(self):
  917. self.ensure_one()
  918. return self.has_group('base.group_public')
  919. def _is_system(self):
  920. self.ensure_one()
  921. return self.has_group('base.group_system')
  922. def _is_admin(self):
  923. self.ensure_one()
  924. return self._is_superuser() or self.has_group('base.group_erp_manager')
  925. def _is_superuser(self):
  926. self.ensure_one()
  927. return self.id == SUPERUSER_ID
  928. @api.model
  929. def get_company_currency_id(self):
  930. return self.env.company.currency_id.id
  931. @tools.ormcache()
  932. def _crypt_context(self):
  933. """ Passlib CryptContext instance used to encrypt and verify
  934. passwords. Can be overridden if technical, legal or political matters
  935. require different kdfs than the provided default.
  936. The work factor of the default KDF can be configured using the
  937. ``password.hashing.rounds`` ICP.
  938. """
  939. cfg = self.env['ir.config_parameter'].sudo()
  940. return CryptContext(
  941. # kdf which can be verified by the context. The default encryption
  942. # kdf is the first of the list
  943. ['pbkdf2_sha512', 'plaintext'],
  944. # deprecated algorithms are still verified as usual, but
  945. # ``needs_update`` will indicate that the stored hash should be
  946. # replaced by a more recent algorithm.
  947. deprecated=['auto'],
  948. pbkdf2_sha512__rounds=max(MIN_ROUNDS, int(cfg.get_param('password.hashing.rounds', 0))),
  949. )
  950. @contextlib.contextmanager
  951. def _assert_can_auth(self, user=None):
  952. """ Checks that the current environment even allows the current auth
  953. request to happen.
  954. The baseline implementation is a simple linear login cooldown: after
  955. a number of failures trying to log-in, the user (by login) is put on
  956. cooldown. During the cooldown period, login *attempts* are ignored
  957. and logged.
  958. :param user: user id or login, for logging purpose
  959. .. warning::
  960. The login counter is not shared between workers and not
  961. specifically thread-safe, the feature exists mostly for
  962. rate-limiting on large number of login attempts (brute-forcing
  963. passwords) so that should not be much of an issue.
  964. For a more complex strategy (e.g. database or distribute storage)
  965. override this method. To simply change the cooldown criteria
  966. (configuration, ...) override _on_login_cooldown instead.
  967. .. note::
  968. This is a *context manager* so it can be called around the login
  969. procedure without having to call it itself.
  970. """
  971. # needs request for remote address
  972. if not request:
  973. yield
  974. return
  975. reg = self.env.registry
  976. failures_map = getattr(reg, '_login_failures', None)
  977. if failures_map is None:
  978. failures_map = reg._login_failures = collections.defaultdict(lambda : (0, datetime.datetime.min))
  979. source = request.httprequest.remote_addr
  980. (failures, previous) = failures_map[source]
  981. if self._on_login_cooldown(failures, previous):
  982. _logger.warning(
  983. "Login attempt ignored for %s (user %r) on %s: "
  984. "%d failures since last success, last failure at %s. "
  985. "You can configure the number of login failures before a "
  986. "user is put on cooldown as well as the duration in the "
  987. "System Parameters. Disable this feature by setting "
  988. "\"base.login_cooldown_after\" to 0.",
  989. source, user or "?", self.env.cr.dbname, failures, previous)
  990. if ipaddress.ip_address(source).is_private:
  991. _logger.warning(
  992. "The rate-limited IP address %s is classified as private "
  993. "and *might* be a proxy. If your Odoo is behind a proxy, "
  994. "it may be mis-configured. Check that you are running "
  995. "Odoo in Proxy Mode and that the proxy is properly configured, see "
  996. "https://www.odoo.com/documentation/16.0/administration/install/deploy.html#https for details.",
  997. source
  998. )
  999. raise AccessDenied(_("Too many login failures, please wait a bit before trying again."))
  1000. try:
  1001. yield
  1002. except AccessDenied:
  1003. (failures, __) = reg._login_failures[source]
  1004. reg._login_failures[source] = (failures + 1, datetime.datetime.now())
  1005. raise
  1006. else:
  1007. reg._login_failures.pop(source, None)
  1008. def _on_login_cooldown(self, failures, previous):
  1009. """ Decides whether the user trying to log in is currently
  1010. "on cooldown" and not even allowed to attempt logging in.
  1011. The default cooldown function simply puts the user on cooldown for
  1012. <login_cooldown_duration> seconds after each failure following the
  1013. <login_cooldown_after>th (0 to disable).
  1014. Can be overridden to implement more complex backoff strategies, or
  1015. e.g. wind down or reset the cooldown period as the previous failure
  1016. recedes into the far past.
  1017. :param int failures: number of recorded failures (since last success)
  1018. :param previous: timestamp of previous failure
  1019. :type previous: datetime.datetime
  1020. :returns: whether the user is currently in cooldown phase (true if cooldown, false if no cooldown and login can continue)
  1021. :rtype: bool
  1022. """
  1023. cfg = self.env['ir.config_parameter'].sudo()
  1024. min_failures = int(cfg.get_param('base.login_cooldown_after', 5))
  1025. if min_failures == 0:
  1026. return False
  1027. delay = int(cfg.get_param('base.login_cooldown_duration', 60))
  1028. return failures >= min_failures and (datetime.datetime.now() - previous) < datetime.timedelta(seconds=delay)
  1029. def _register_hook(self):
  1030. if hasattr(self, 'check_credentials'):
  1031. _logger.warning("The check_credentials method of res.users has been renamed _check_credentials. One of your installed modules defines one, but it will not be called anymore.")
  1032. def _mfa_type(self):
  1033. """ If an MFA method is enabled, returns its type as a string. """
  1034. return
  1035. def _mfa_url(self):
  1036. """ If an MFA method is enabled, returns the URL for its second step. """
  1037. return
  1038. #
  1039. # Implied groups
  1040. #
  1041. # Extension of res.groups and res.users with a relation for "implied" or
  1042. # "inherited" groups. Once a user belongs to a group, it automatically belongs
  1043. # to the implied groups (transitively).
  1044. #
  1045. class GroupsImplied(models.Model):
  1046. _inherit = 'res.groups'
  1047. implied_ids = fields.Many2many('res.groups', 'res_groups_implied_rel', 'gid', 'hid',
  1048. string='Inherits', help='Users of this group automatically inherit those groups')
  1049. trans_implied_ids = fields.Many2many('res.groups', string='Transitively inherits',
  1050. compute='_compute_trans_implied', recursive=True)
  1051. @api.depends('implied_ids.trans_implied_ids')
  1052. def _compute_trans_implied(self):
  1053. # Compute the transitive closure recursively. Note that the performance
  1054. # is good, because the record cache behaves as a memo (the field is
  1055. # never computed twice on a given group.)
  1056. for g in self:
  1057. g.trans_implied_ids = g.implied_ids | g.implied_ids.trans_implied_ids
  1058. @api.model_create_multi
  1059. def create(self, vals_list):
  1060. user_ids_list = [vals.pop('users', None) for vals in vals_list]
  1061. groups = super(GroupsImplied, self).create(vals_list)
  1062. for group, user_ids in zip(groups, user_ids_list):
  1063. if user_ids:
  1064. # delegate addition of users to add implied groups
  1065. group.write({'users': user_ids})
  1066. return groups
  1067. def write(self, values):
  1068. res = super(GroupsImplied, self).write(values)
  1069. if values.get('users') or values.get('implied_ids'):
  1070. # add all implied groups (to all users of each group)
  1071. for group in self:
  1072. self._cr.execute("""
  1073. WITH RECURSIVE group_imply(gid, hid) AS (
  1074. SELECT gid, hid
  1075. FROM res_groups_implied_rel
  1076. UNION
  1077. SELECT i.gid, r.hid
  1078. FROM res_groups_implied_rel r
  1079. JOIN group_imply i ON (i.hid = r.gid)
  1080. )
  1081. INSERT INTO res_groups_users_rel (gid, uid)
  1082. SELECT i.hid, r.uid
  1083. FROM group_imply i, res_groups_users_rel r
  1084. WHERE r.gid = i.gid
  1085. AND i.gid = %(gid)s
  1086. EXCEPT
  1087. SELECT r.gid, r.uid
  1088. FROM res_groups_users_rel r
  1089. JOIN group_imply i ON (r.gid = i.hid)
  1090. WHERE i.gid = %(gid)s
  1091. """, dict(gid=group.id))
  1092. self._check_one_user_type()
  1093. return res
  1094. def _apply_group(self, implied_group):
  1095. """ Add the given group to the groups implied by the current group
  1096. :param implied_group: the implied group to add
  1097. """
  1098. groups = self.filtered(lambda g: implied_group not in g.implied_ids)
  1099. groups.write({'implied_ids': [Command.link(implied_group.id)]})
  1100. def _remove_group(self, implied_group):
  1101. """ Remove the given group from the implied groups of the current group
  1102. :param implied_group: the implied group to remove
  1103. """
  1104. groups = self.filtered(lambda g: implied_group in g.implied_ids)
  1105. if groups:
  1106. groups.write({'implied_ids': [Command.unlink(implied_group.id)]})
  1107. # if user belongs to implied_group thanks to another group, don't remove him
  1108. # this avoids readding the template user and triggering the mechanism at 121cd0d6084cb28
  1109. users_to_unlink = [
  1110. user
  1111. for user in groups.with_context(active_test=False).users
  1112. if implied_group not in (user.groups_id - implied_group).trans_implied_ids
  1113. ]
  1114. if users_to_unlink:
  1115. # do not remove inactive users (e.g. default)
  1116. implied_group.with_context(active_test=False).write(
  1117. {'users': [Command.unlink(user.id) for user in users_to_unlink]})
  1118. class UsersImplied(models.Model):
  1119. _inherit = 'res.users'
  1120. @api.model_create_multi
  1121. def create(self, vals_list):
  1122. for values in vals_list:
  1123. if 'groups_id' in values:
  1124. # complete 'groups_id' with implied groups
  1125. user = self.new(values)
  1126. gs = user.groups_id._origin
  1127. gs = gs | gs.trans_implied_ids
  1128. values['groups_id'] = type(self).groups_id.convert_to_write(gs, user)
  1129. return super(UsersImplied, self).create(vals_list)
  1130. def write(self, values):
  1131. if not values.get('groups_id'):
  1132. return super(UsersImplied, self).write(values)
  1133. users_before = self.filtered(lambda u: u._is_internal())
  1134. res = super(UsersImplied, self).write(values)
  1135. demoted_users = users_before.filtered(lambda u: not u._is_internal())
  1136. if demoted_users:
  1137. # demoted users are restricted to the assigned groups only
  1138. vals = {'groups_id': [Command.clear()] + values['groups_id']}
  1139. super(UsersImplied, demoted_users).write(vals)
  1140. # add implied groups for all users (in batches)
  1141. users_batch = defaultdict(self.browse)
  1142. for user in self:
  1143. users_batch[user.groups_id] += user
  1144. for groups, users in users_batch.items():
  1145. gs = set(concat(g.trans_implied_ids for g in groups))
  1146. vals = {'groups_id': [Command.link(g.id) for g in gs]}
  1147. super(UsersImplied, users).write(vals)
  1148. return res
  1149. #
  1150. # Virtual checkbox and selection for res.user form view
  1151. #
  1152. # Extension of res.groups and res.users for the special groups view in the users
  1153. # form. This extension presents groups with selection and boolean widgets:
  1154. # - Groups are shown by application, with boolean and/or selection fields.
  1155. # Selection fields typically defines a role "Name" for the given application.
  1156. # - Uncategorized groups are presented as boolean fields and grouped in a
  1157. # section "Others".
  1158. #
  1159. # The user form view is modified by an inherited view (base.user_groups_view);
  1160. # the inherited view replaces the field 'groups_id' by a set of reified group
  1161. # fields (boolean or selection fields). The arch of that view is regenerated
  1162. # each time groups are changed.
  1163. #
  1164. # Naming conventions for reified groups fields:
  1165. # - boolean field 'in_group_ID' is True iff
  1166. # ID is in 'groups_id'
  1167. # - selection field 'sel_groups_ID1_..._IDk' is ID iff
  1168. # ID is in 'groups_id' and ID is maximal in the set {ID1, ..., IDk}
  1169. #
  1170. class GroupsView(models.Model):
  1171. _inherit = 'res.groups'
  1172. @api.model_create_multi
  1173. def create(self, vals_list):
  1174. groups = super().create(vals_list)
  1175. self._update_user_groups_view()
  1176. # actions.get_bindings() depends on action records
  1177. self.env['ir.actions.actions'].clear_caches()
  1178. return groups
  1179. def write(self, values):
  1180. # determine which values the "user groups view" depends on
  1181. VIEW_DEPS = ('category_id', 'implied_ids')
  1182. view_values0 = [g[name] for name in VIEW_DEPS if name in values for g in self]
  1183. res = super(GroupsView, self).write(values)
  1184. # update the "user groups view" only if necessary
  1185. view_values1 = [g[name] for name in VIEW_DEPS if name in values for g in self]
  1186. if view_values0 != view_values1:
  1187. self._update_user_groups_view()
  1188. # actions.get_bindings() depends on action records
  1189. self.env['ir.actions.actions'].clear_caches()
  1190. return res
  1191. def unlink(self):
  1192. res = super(GroupsView, self).unlink()
  1193. self._update_user_groups_view()
  1194. # actions.get_bindings() depends on action records
  1195. self.env['ir.actions.actions'].clear_caches()
  1196. return res
  1197. def _get_hidden_extra_categories(self):
  1198. return ['base.module_category_hidden', 'base.module_category_extra', 'base.module_category_usability']
  1199. @api.model
  1200. def _update_user_groups_view(self):
  1201. """ Modify the view with xmlid ``base.user_groups_view``, which inherits
  1202. the user form view, and introduces the reified group fields.
  1203. """
  1204. # remove the language to avoid translations, it will be handled at the view level
  1205. self = self.with_context(lang=None)
  1206. # We have to try-catch this, because at first init the view does not
  1207. # exist but we are already creating some basic groups.
  1208. view = self.env.ref('base.user_groups_view', raise_if_not_found=False)
  1209. if not (view and view._name == 'ir.ui.view'):
  1210. return
  1211. if self._context.get('install_filename') or self._context.get(MODULE_UNINSTALL_FLAG):
  1212. # use a dummy view during install/upgrade/uninstall
  1213. xml = E.field(name="groups_id", position="after")
  1214. else:
  1215. group_no_one = view.env.ref('base.group_no_one')
  1216. group_employee = view.env.ref('base.group_user')
  1217. xml0, xml1, xml2, xml3, xml4 = [], [], [], [], []
  1218. xml_by_category = {}
  1219. xml1.append(E.separator(string='User Type', colspan="2", groups='base.group_no_one'))
  1220. user_type_field_name = ''
  1221. user_type_readonly = str({})
  1222. sorted_tuples = sorted(self.get_groups_by_application(),
  1223. key=lambda t: t[0].xml_id != 'base.module_category_user_type')
  1224. for app, kind, gs, category_name in sorted_tuples: # we process the user type first
  1225. attrs = {}
  1226. # hide groups in categories 'Hidden' and 'Extra' (except for group_no_one)
  1227. if app.xml_id in self._get_hidden_extra_categories():
  1228. attrs['groups'] = 'base.group_no_one'
  1229. # User type (employee, portal or public) is a separated group. This is the only 'selection'
  1230. # group of res.groups without implied groups (with each other).
  1231. if app.xml_id == 'base.module_category_user_type':
  1232. # application name with a selection field
  1233. field_name = name_selection_groups(gs.ids)
  1234. # test_reified_groups, put the user category type in invisible
  1235. # as it's used in domain of attrs of other fields,
  1236. # and the normal user category type field node is wrapped in a `groups="base.no_one"`,
  1237. # and is therefore removed when not in debug mode.
  1238. xml0.append(E.field(name=field_name, invisible="1", on_change="1"))
  1239. user_type_field_name = field_name
  1240. user_type_readonly = str({'readonly': [(user_type_field_name, '!=', group_employee.id)]})
  1241. attrs['widget'] = 'radio'
  1242. # Trigger the on_change of this "virtual field"
  1243. attrs['on_change'] = '1'
  1244. xml1.append(E.field(name=field_name, **attrs))
  1245. xml1.append(E.newline())
  1246. elif kind == 'selection':
  1247. # application name with a selection field
  1248. field_name = name_selection_groups(gs.ids)
  1249. attrs['attrs'] = user_type_readonly
  1250. attrs['on_change'] = '1'
  1251. if category_name not in xml_by_category:
  1252. xml_by_category[category_name] = []
  1253. xml_by_category[category_name].append(E.newline())
  1254. xml_by_category[category_name].append(E.field(name=field_name, **attrs))
  1255. xml_by_category[category_name].append(E.newline())
  1256. # add duplicate invisible field so default values are saved on create
  1257. if attrs.get('groups') == 'base.group_no_one':
  1258. xml0.append(E.field(name=field_name, **dict(attrs, invisible="1", groups='!base.group_no_one')))
  1259. else:
  1260. # application separator with boolean fields
  1261. app_name = app.name or 'Other'
  1262. xml4.append(E.separator(string=app_name, **attrs))
  1263. left_group, right_group = [], []
  1264. attrs['attrs'] = user_type_readonly
  1265. # we can't use enumerate, as we sometime skip groups
  1266. group_count = 0
  1267. for g in gs:
  1268. field_name = name_boolean_group(g.id)
  1269. dest_group = left_group if group_count % 2 == 0 else right_group
  1270. if g == group_no_one:
  1271. # make the group_no_one invisible in the form view
  1272. dest_group.append(E.field(name=field_name, invisible="1", **attrs))
  1273. else:
  1274. dest_group.append(E.field(name=field_name, **attrs))
  1275. # add duplicate invisible field so default values are saved on create
  1276. xml0.append(E.field(name=field_name, **dict(attrs, invisible="1", groups='!base.group_no_one')))
  1277. group_count += 1
  1278. xml4.append(E.group(*left_group))
  1279. xml4.append(E.group(*right_group))
  1280. xml4.append({'class': "o_label_nowrap"})
  1281. if user_type_field_name:
  1282. user_type_attrs = {'invisible': [(user_type_field_name, '!=', group_employee.id)]}
  1283. else:
  1284. user_type_attrs = {}
  1285. for xml_cat in sorted(xml_by_category.keys(), key=lambda it: it[0]):
  1286. master_category_name = xml_cat[1]
  1287. xml3.append(E.group(*(xml_by_category[xml_cat]), string=master_category_name))
  1288. field_name = 'user_group_warning'
  1289. user_group_warning_xml = E.div({
  1290. 'class': "alert alert-warning",
  1291. 'role': "alert",
  1292. 'colspan': "2",
  1293. 'attrs': str({'invisible': [(field_name, '=', False)]})
  1294. })
  1295. user_group_warning_xml.append(E.label({
  1296. 'for': field_name,
  1297. 'string': "Access Rights Mismatch",
  1298. 'class': "text text-warning fw-bold",
  1299. }))
  1300. user_group_warning_xml.append(E.field(name=field_name))
  1301. xml2.append(user_group_warning_xml)
  1302. xml = E.field(
  1303. *(xml0),
  1304. E.group(*(xml1), groups="base.group_no_one"),
  1305. E.group(*(xml2), attrs=str(user_type_attrs)),
  1306. E.group(*(xml3), attrs=str(user_type_attrs)),
  1307. E.group(*(xml4), attrs=str(user_type_attrs), groups="base.group_no_one"), name="groups_id", position="replace")
  1308. xml.addprevious(etree.Comment("GENERATED AUTOMATICALLY BY GROUPS"))
  1309. # serialize and update the view
  1310. xml_content = etree.tostring(xml, pretty_print=True, encoding="unicode")
  1311. if xml_content != view.arch: # avoid useless xml validation if no change
  1312. new_context = dict(view._context)
  1313. new_context.pop('install_filename', None) # don't set arch_fs for this computed view
  1314. new_context['lang'] = None
  1315. view.with_context(new_context).write({'arch': xml_content})
  1316. def get_application_groups(self, domain):
  1317. """ Return the non-share groups that satisfy ``domain``. """
  1318. return self.search(domain + [('share', '=', False)])
  1319. @api.model
  1320. def get_groups_by_application(self):
  1321. """ Return all groups classified by application (module category), as a list::
  1322. [(app, kind, groups), ...],
  1323. where ``app`` and ``groups`` are recordsets, and ``kind`` is either
  1324. ``'boolean'`` or ``'selection'``. Applications are given in sequence
  1325. order. If ``kind`` is ``'selection'``, ``groups`` are given in
  1326. reverse implication order.
  1327. """
  1328. def linearize(app, gs, category_name):
  1329. # 'User Type' is an exception
  1330. if app.xml_id == 'base.module_category_user_type':
  1331. return (app, 'selection', gs.sorted('id'), category_name)
  1332. # determine sequence order: a group appears after its implied groups
  1333. order = {g: len(g.trans_implied_ids & gs) for g in gs}
  1334. # We want a selection for Accounting too. Auditor and Invoice are both
  1335. # children of Accountant, but the two of them make a full accountant
  1336. # so it makes no sense to have checkboxes.
  1337. if app.xml_id == 'base.module_category_accounting_accounting':
  1338. return (app, 'selection', gs.sorted(key=order.get), category_name)
  1339. # check whether order is total, i.e., sequence orders are distinct
  1340. if len(set(order.values())) == len(gs):
  1341. return (app, 'selection', gs.sorted(key=order.get), category_name)
  1342. else:
  1343. return (app, 'boolean', gs, (100, 'Other'))
  1344. # classify all groups by application
  1345. by_app, others = defaultdict(self.browse), self.browse()
  1346. for g in self.get_application_groups([]):
  1347. if g.category_id:
  1348. by_app[g.category_id] += g
  1349. else:
  1350. others += g
  1351. # build the result
  1352. res = []
  1353. for app, gs in sorted(by_app.items(), key=lambda it: it[0].sequence or 0):
  1354. if app.parent_id:
  1355. res.append(linearize(app, gs, (app.parent_id.sequence, app.parent_id.name)))
  1356. else:
  1357. res.append(linearize(app, gs, (100, 'Other')))
  1358. if others:
  1359. res.append((self.env['ir.module.category'], 'boolean', others, (100,'Other')))
  1360. return res
  1361. class ModuleCategory(models.Model):
  1362. _inherit = "ir.module.category"
  1363. def write(self, values):
  1364. res = super().write(values)
  1365. if "name" in values:
  1366. self.env["res.groups"]._update_user_groups_view()
  1367. return res
  1368. def unlink(self):
  1369. res = super().unlink()
  1370. self.env["res.groups"]._update_user_groups_view()
  1371. return res
  1372. class UsersView(models.Model):
  1373. _inherit = 'res.users'
  1374. user_group_warning = fields.Text(string="User Group Warning", compute="_compute_user_group_warning")
  1375. @api.depends('groups_id', 'share')
  1376. @api.depends_context('show_user_group_warning')
  1377. def _compute_user_group_warning(self):
  1378. self.user_group_warning = False
  1379. if self._context.get('show_user_group_warning'):
  1380. for user in self.filtered_domain([('share', '=', False)]):
  1381. group_inheritance_warnings = self._prepare_warning_for_group_inheritance(user)
  1382. if group_inheritance_warnings:
  1383. user.user_group_warning = group_inheritance_warnings
  1384. @api.model_create_multi
  1385. def create(self, vals_list):
  1386. new_vals_list = []
  1387. for values in vals_list:
  1388. new_vals_list.append(self._remove_reified_groups(values))
  1389. users = super(UsersView, self).create(new_vals_list)
  1390. group_multi_company_id = self.env['ir.model.data']._xmlid_to_res_id(
  1391. 'base.group_multi_company', raise_if_not_found=False)
  1392. if group_multi_company_id:
  1393. for user in users:
  1394. if len(user.company_ids) <= 1 and group_multi_company_id in user.groups_id.ids:
  1395. user.write({'groups_id': [Command.unlink(group_multi_company_id)]})
  1396. elif len(user.company_ids) > 1 and group_multi_company_id not in user.groups_id.ids:
  1397. user.write({'groups_id': [Command.link(group_multi_company_id)]})
  1398. return users
  1399. def write(self, values):
  1400. values = self._remove_reified_groups(values)
  1401. res = super(UsersView, self).write(values)
  1402. if 'company_ids' not in values:
  1403. return res
  1404. group_multi_company = self.env.ref('base.group_multi_company', False)
  1405. if group_multi_company:
  1406. for user in self:
  1407. if len(user.company_ids) <= 1 and user.id in group_multi_company.users.ids:
  1408. user.write({'groups_id': [Command.unlink(group_multi_company.id)]})
  1409. elif len(user.company_ids) > 1 and user.id not in group_multi_company.users.ids:
  1410. user.write({'groups_id': [Command.link(group_multi_company.id)]})
  1411. return res
  1412. @api.model
  1413. def new(self, values=None, origin=None, ref=None):
  1414. if values is None:
  1415. values = {}
  1416. values = self._remove_reified_groups(values)
  1417. user = super().new(values=values, origin=origin, ref=ref)
  1418. group_multi_company = self.env.ref('base.group_multi_company', False)
  1419. if group_multi_company and 'company_ids' in values:
  1420. if len(user.company_ids) <= 1 and user.id in group_multi_company.users.ids:
  1421. user.update({'groups_id': [Command.unlink(group_multi_company.id)]})
  1422. elif len(user.company_ids) > 1 and user.id not in group_multi_company.users.ids:
  1423. user.update({'groups_id': [Command.link(group_multi_company.id)]})
  1424. return user
  1425. def _prepare_warning_for_group_inheritance(self, user):
  1426. """ Check (updated) groups configuration for user. If implieds groups
  1427. will be added back due to inheritance and hierarchy in groups return
  1428. a message explaining the missing groups.
  1429. :param res.users user: target user
  1430. :return: string to display in a warning
  1431. """
  1432. # Current groups of the user
  1433. current_groups = user.groups_id.filtered('trans_implied_ids')
  1434. current_groups_by_category = defaultdict(lambda: self.env['res.groups'])
  1435. for group in current_groups:
  1436. current_groups_by_category[group.category_id] |= group.trans_implied_ids.filtered(lambda grp: grp.category_id == group.category_id)
  1437. missing_groups = {}
  1438. # We don't want to show warning for "Technical" and "Extra Rights" groups
  1439. categories_to_ignore = self.env.ref('base.module_category_hidden') + self.env.ref('base.module_category_usability')
  1440. for group in current_groups:
  1441. # Get the updated group from current groups
  1442. missing_implied_groups = group.implied_ids - user.groups_id
  1443. # Get the missing group needed in updated group's category (For example, someone changes
  1444. # Sales: Admin to Sales: User, but Field Service is already set to Admin, so here in the
  1445. # 'Sales' category, we will at the minimum need Admin group)
  1446. missing_implied_groups = missing_implied_groups.filtered(
  1447. lambda g:
  1448. g.category_id not in (group.category_id | categories_to_ignore) and
  1449. g not in current_groups_by_category[g.category_id] and
  1450. (self.user_has_groups('base.group_no_one') or g.category_id)
  1451. )
  1452. if missing_implied_groups:
  1453. # prepare missing group message, by categories
  1454. missing_groups[group] = ", ".join(f'"{missing_group.category_id.name or _("Other")}: {missing_group.name}"'
  1455. for missing_group in missing_implied_groups)
  1456. return "\n".join(
  1457. _('Since %(user)s is a/an "%(category)s: %(group)s", they will at least obtain the right %(missing_group_message)s',
  1458. user=user.name,
  1459. category=group.category_id.name or _('Other'),
  1460. group=group.name,
  1461. missing_group_message=missing_group_message
  1462. ) for group, missing_group_message in missing_groups.items()
  1463. )
  1464. def _remove_reified_groups(self, values):
  1465. """ return `values` without reified group fields """
  1466. add, rem = [], []
  1467. values1 = {}
  1468. for key, val in values.items():
  1469. if is_boolean_group(key):
  1470. (add if val else rem).append(get_boolean_group(key))
  1471. elif is_selection_groups(key):
  1472. rem += get_selection_groups(key)
  1473. if val:
  1474. add.append(val)
  1475. else:
  1476. values1[key] = val
  1477. if 'groups_id' not in values and (add or rem):
  1478. added = self.env['res.groups'].sudo().browse(add)
  1479. added |= added.mapped('trans_implied_ids')
  1480. added_ids = added._ids
  1481. # remove group ids in `rem` and add group ids in `add`
  1482. # do not remove groups that are added by implied
  1483. values1['groups_id'] = list(itertools.chain(
  1484. zip(repeat(3), [gid for gid in rem if gid not in added_ids]),
  1485. zip(repeat(4), add)
  1486. ))
  1487. return values1
  1488. @api.model
  1489. def default_get(self, fields):
  1490. group_fields, fields = partition(is_reified_group, fields)
  1491. fields1 = (fields + ['groups_id']) if group_fields else fields
  1492. values = super(UsersView, self).default_get(fields1)
  1493. self._add_reified_groups(group_fields, values)
  1494. return values
  1495. def onchange(self, values, field_name, field_onchange):
  1496. # field_name can be either a string, a list or Falsy
  1497. if isinstance(field_name, list):
  1498. names = field_name
  1499. elif field_name:
  1500. names = [field_name]
  1501. else:
  1502. names = []
  1503. if any(is_reified_group(field) for field in names):
  1504. field_name = (
  1505. ['groups_id']
  1506. + [field for field in names if not is_reified_group(field)]
  1507. )
  1508. values.pop('groups_id', None)
  1509. values.update(self._remove_reified_groups(values))
  1510. field_onchange['groups_id'] = ''
  1511. result = super().onchange(values, field_name, field_onchange)
  1512. if not field_name: # merged default_get
  1513. self._add_reified_groups(
  1514. filter(is_reified_group, field_onchange),
  1515. result.setdefault('value', {})
  1516. )
  1517. return result
  1518. def read(self, fields=None, load='_classic_read'):
  1519. # determine whether reified groups fields are required, and which ones
  1520. fields1 = fields or list(self.fields_get())
  1521. group_fields, other_fields = partition(is_reified_group, fields1)
  1522. # read regular fields (other_fields); add 'groups_id' if necessary
  1523. drop_groups_id = False
  1524. if group_fields and fields:
  1525. if 'groups_id' not in other_fields:
  1526. other_fields.append('groups_id')
  1527. drop_groups_id = True
  1528. else:
  1529. other_fields = fields
  1530. res = super(UsersView, self).read(other_fields, load=load)
  1531. # post-process result to add reified group fields
  1532. if group_fields:
  1533. for values in res:
  1534. self._add_reified_groups(group_fields, values)
  1535. if drop_groups_id:
  1536. values.pop('groups_id', None)
  1537. return res
  1538. @api.model
  1539. def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
  1540. if fields:
  1541. # ignore reified fields
  1542. fields = [fname for fname in fields if not is_reified_group(fname)]
  1543. return super().read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)
  1544. def _add_reified_groups(self, fields, values):
  1545. """ add the given reified group fields into `values` """
  1546. gids = set(parse_m2m(values.get('groups_id') or []))
  1547. for f in fields:
  1548. if is_boolean_group(f):
  1549. values[f] = get_boolean_group(f) in gids
  1550. elif is_selection_groups(f):
  1551. # determine selection groups, in order
  1552. sel_groups = self.env['res.groups'].sudo().browse(get_selection_groups(f))
  1553. sel_order = {g: len(g.trans_implied_ids & sel_groups) for g in sel_groups}
  1554. sel_groups = sel_groups.sorted(key=sel_order.get)
  1555. # determine which ones are in gids
  1556. selected = [gid for gid in sel_groups.ids if gid in gids]
  1557. # if 'Internal User' is in the group, this is the "User Type" group
  1558. # and we need to show 'Internal User' selected, not Public/Portal.
  1559. if self.env.ref('base.group_user').id in selected:
  1560. values[f] = self.env.ref('base.group_user').id
  1561. else:
  1562. values[f] = selected and selected[-1] or False
  1563. @api.model
  1564. def fields_get(self, allfields=None, attributes=None):
  1565. res = super(UsersView, self).fields_get(allfields, attributes=attributes)
  1566. # add reified groups fields
  1567. for app, kind, gs, category_name in self.env['res.groups'].sudo().get_groups_by_application():
  1568. if kind == 'selection':
  1569. # 'User Type' should not be 'False'. A user is either 'employee', 'portal' or 'public' (required).
  1570. selection_vals = [(False, '')]
  1571. if app.xml_id == 'base.module_category_user_type':
  1572. selection_vals = []
  1573. field_name = name_selection_groups(gs.ids)
  1574. if allfields and field_name not in allfields:
  1575. continue
  1576. # selection group field
  1577. tips = []
  1578. if app.description:
  1579. tips.append(app.description + '\n')
  1580. tips.extend('%s: %s' % (g.name, g.comment) for g in gs if g.comment)
  1581. res[field_name] = {
  1582. 'type': 'selection',
  1583. 'string': app.name or _('Other'),
  1584. 'selection': selection_vals + [(g.id, g.name) for g in gs],
  1585. 'help': '\n'.join(tips),
  1586. 'exportable': False,
  1587. 'selectable': False,
  1588. }
  1589. else:
  1590. # boolean group fields
  1591. for g in gs:
  1592. field_name = name_boolean_group(g.id)
  1593. if allfields and field_name not in allfields:
  1594. continue
  1595. res[field_name] = {
  1596. 'type': 'boolean',
  1597. 'string': g.name,
  1598. 'help': g.comment,
  1599. 'exportable': False,
  1600. 'selectable': False,
  1601. }
  1602. # add self readable/writable fields
  1603. missing = set(self.SELF_WRITEABLE_FIELDS).union(self.SELF_READABLE_FIELDS).difference(res.keys())
  1604. if allfields:
  1605. missing = missing.intersection(allfields)
  1606. if missing:
  1607. res.update({
  1608. key: dict(values, readonly=key not in self.SELF_WRITEABLE_FIELDS, searchable=False)
  1609. for key, values in super(UsersView, self.sudo()).fields_get(missing, attributes).items()
  1610. })
  1611. return res
  1612. class CheckIdentity(models.TransientModel):
  1613. """ Wizard used to re-check the user's credentials (password)
  1614. Might be useful before the more security-sensitive operations, users might be
  1615. leaving their computer unlocked & unattended. Re-checking credentials mitigates
  1616. some of the risk of a third party using such an unattended device to manipulate
  1617. the account.
  1618. """
  1619. _name = 'res.users.identitycheck'
  1620. _description = "Password Check Wizard"
  1621. request = fields.Char(readonly=True, groups=fields.NO_ACCESS)
  1622. password = fields.Char()
  1623. def run_check(self):
  1624. assert request, "This method can only be accessed over HTTP"
  1625. try:
  1626. self.create_uid._check_credentials(self.password, {'interactive': True})
  1627. except AccessDenied:
  1628. raise UserError(_("Incorrect Password, try again or click on Forgot Password to reset your password."))
  1629. self.password = False
  1630. request.session['identity-check-last'] = time.time()
  1631. ctx, model, ids, method = json.loads(self.sudo().request)
  1632. method = getattr(self.env(context=ctx)[model].browse(ids), method)
  1633. assert getattr(method, '__has_check_identity', False)
  1634. return method()
  1635. #----------------------------------------------------------
  1636. # change password wizard
  1637. #----------------------------------------------------------
  1638. class ChangePasswordWizard(models.TransientModel):
  1639. """ A wizard to manage the change of users' passwords. """
  1640. _name = "change.password.wizard"
  1641. _description = "Change Password Wizard"
  1642. def _default_user_ids(self):
  1643. user_ids = self._context.get('active_model') == 'res.users' and self._context.get('active_ids') or []
  1644. return [
  1645. Command.create({'user_id': user.id, 'user_login': user.login})
  1646. for user in self.env['res.users'].browse(user_ids)
  1647. ]
  1648. user_ids = fields.One2many('change.password.user', 'wizard_id', string='Users', default=_default_user_ids)
  1649. def change_password_button(self):
  1650. self.ensure_one()
  1651. self.user_ids.change_password_button()
  1652. if self.env.user in self.user_ids.user_id:
  1653. return {'type': 'ir.actions.client', 'tag': 'reload'}
  1654. return {'type': 'ir.actions.act_window_close'}
  1655. class ChangePasswordUser(models.TransientModel):
  1656. """ A model to configure users in the change password wizard. """
  1657. _name = 'change.password.user'
  1658. _description = 'User, Change Password Wizard'
  1659. wizard_id = fields.Many2one('change.password.wizard', string='Wizard', required=True, ondelete='cascade')
  1660. user_id = fields.Many2one('res.users', string='User', required=True, ondelete='cascade')
  1661. user_login = fields.Char(string='User Login', readonly=True)
  1662. new_passwd = fields.Char(string='New Password', default='')
  1663. def change_password_button(self):
  1664. for line in self:
  1665. if not line.new_passwd:
  1666. raise UserError(_("Before clicking on 'Change Password', you have to write a new password."))
  1667. line.user_id._change_password(line.new_passwd)
  1668. # don't keep temporary passwords in the database longer than necessary
  1669. self.write({'new_passwd': False})
  1670. class ChangePasswordOwn(models.TransientModel):
  1671. _name = "change.password.own"
  1672. _description = "User, change own password wizard"
  1673. _transient_max_hours = 0.1
  1674. new_password = fields.Char(string="New Password")
  1675. confirm_password = fields.Char(string="New Password (Confirmation)")
  1676. @api.constrains('new_password', 'confirm_password')
  1677. def _check_password_confirmation(self):
  1678. if self.confirm_password != self.new_password:
  1679. raise ValidationError(_("The new password and its confirmation must be identical."))
  1680. @check_identity
  1681. def change_password(self):
  1682. self.env.user._change_password(self.new_password)
  1683. self.unlink()
  1684. # reload to avoid a session expired error
  1685. # would be great to update the session id in-place, but it seems dicey
  1686. return {'type': 'ir.actions.client', 'tag': 'reload'}
  1687. # API keys support
  1688. API_KEY_SIZE = 20 # in bytes
  1689. INDEX_SIZE = 8 # in hex digits, so 4 bytes, or 20% of the key
  1690. KEY_CRYPT_CONTEXT = CryptContext(
  1691. # default is 29000 rounds which is 25~50ms, which is probably unnecessary
  1692. # given in this case all the keys are completely random data: dictionary
  1693. # attacks on API keys isn't much of a concern
  1694. ['pbkdf2_sha512'], pbkdf2_sha512__rounds=6000,
  1695. )
  1696. class APIKeysUser(models.Model):
  1697. _inherit = 'res.users'
  1698. api_key_ids = fields.One2many('res.users.apikeys', 'user_id', string="API Keys")
  1699. @property
  1700. def SELF_READABLE_FIELDS(self):
  1701. return super().SELF_READABLE_FIELDS + ['api_key_ids']
  1702. @property
  1703. def SELF_WRITEABLE_FIELDS(self):
  1704. return super().SELF_WRITEABLE_FIELDS + ['api_key_ids']
  1705. def _rpc_api_keys_only(self):
  1706. """ To be overridden if RPC access needs to be restricted to API keys, e.g. for 2FA """
  1707. return False
  1708. def _check_credentials(self, password, user_agent_env):
  1709. user_agent_env = user_agent_env or {}
  1710. if user_agent_env.get('interactive', True):
  1711. if 'interactive' not in user_agent_env:
  1712. _logger.warning(
  1713. "_check_credentials without 'interactive' env key, assuming interactive login. \
  1714. Check calls and overrides to ensure the 'interactive' key is properly set in \
  1715. all _check_credentials environments"
  1716. )
  1717. return super()._check_credentials(password, user_agent_env)
  1718. if not self.env.user._rpc_api_keys_only():
  1719. try:
  1720. return super()._check_credentials(password, user_agent_env)
  1721. except AccessDenied:
  1722. pass
  1723. # 'rpc' scope does not really exist, we basically require a global key (scope NULL)
  1724. if self.env['res.users.apikeys']._check_credentials(scope='rpc', key=password) == self.env.uid:
  1725. return
  1726. raise AccessDenied()
  1727. @check_identity
  1728. def api_key_wizard(self):
  1729. return {
  1730. 'type': 'ir.actions.act_window',
  1731. 'res_model': 'res.users.apikeys.description',
  1732. 'name': 'New API Key',
  1733. 'target': 'new',
  1734. 'views': [(False, 'form')],
  1735. }
  1736. class APIKeys(models.Model):
  1737. _name = 'res.users.apikeys'
  1738. _description = 'Users API Keys'
  1739. _auto = False # so we can have a secret column
  1740. name = fields.Char("Description", required=True, readonly=True)
  1741. user_id = fields.Many2one('res.users', index=True, required=True, readonly=True, ondelete="cascade")
  1742. scope = fields.Char("Scope", readonly=True)
  1743. create_date = fields.Datetime("Creation Date", readonly=True)
  1744. def init(self):
  1745. table = sql.Identifier(self._table)
  1746. self.env.cr.execute(sql.SQL("""
  1747. CREATE TABLE IF NOT EXISTS {table} (
  1748. id serial primary key,
  1749. name varchar not null,
  1750. user_id integer not null REFERENCES res_users(id),
  1751. scope varchar,
  1752. index varchar({index_size}) not null CHECK (char_length(index) = {index_size}),
  1753. key varchar not null,
  1754. create_date timestamp without time zone DEFAULT (now() at time zone 'utc')
  1755. )
  1756. """).format(table=table, index_size=sql.Placeholder('index_size')), {
  1757. 'index_size': INDEX_SIZE
  1758. })
  1759. index_name = self._table + "_user_id_index_idx"
  1760. if len(index_name) > 63:
  1761. # unique determinist index name
  1762. index_name = self._table[:50] + "_idx_" + sha256(self._table.encode()).hexdigest()[:8]
  1763. self.env.cr.execute(sql.SQL("""
  1764. CREATE INDEX IF NOT EXISTS {index_name} ON {table} (user_id, index);
  1765. """).format(
  1766. table=table,
  1767. index_name=sql.Identifier(index_name)
  1768. ))
  1769. @check_identity
  1770. def remove(self):
  1771. return self._remove()
  1772. def _remove(self):
  1773. """Use the remove() method to remove an API Key. This method implement logic,
  1774. but won't check the identity (mainly used to remove trusted devices)"""
  1775. if not self:
  1776. return {'type': 'ir.actions.act_window_close'}
  1777. if self.env.is_system() or self.mapped('user_id') == self.env.user:
  1778. ip = request.httprequest.environ['REMOTE_ADDR'] if request else 'n/a'
  1779. _logger.info("API key(s) removed: scope: <%s> for '%s' (#%s) from %s",
  1780. self.mapped('scope'), self.env.user.login, self.env.uid, ip)
  1781. self.sudo().unlink()
  1782. return {'type': 'ir.actions.act_window_close'}
  1783. raise AccessError(_("You can not remove API keys unless they're yours or you are a system user"))
  1784. def _check_credentials(self, *, scope, key):
  1785. assert scope, "scope is required"
  1786. index = key[:INDEX_SIZE]
  1787. self.env.cr.execute('''
  1788. SELECT user_id, key
  1789. FROM {} INNER JOIN res_users u ON (u.id = user_id)
  1790. WHERE u.active and index = %s AND (scope IS NULL OR scope = %s)
  1791. '''.format(self._table),
  1792. [index, scope])
  1793. for user_id, current_key in self.env.cr.fetchall():
  1794. if KEY_CRYPT_CONTEXT.verify(key, current_key):
  1795. return user_id
  1796. def _generate(self, scope, name):
  1797. """Generates an api key.
  1798. :param str scope: the scope of the key. If None, the key will give access to any rpc.
  1799. :param str name: the name of the key, mainly intended to be displayed in the UI.
  1800. :return: str: the key.
  1801. """
  1802. # no need to clear the LRU when *adding* a key, only when removing
  1803. k = binascii.hexlify(os.urandom(API_KEY_SIZE)).decode()
  1804. self.env.cr.execute("""
  1805. INSERT INTO {table} (name, user_id, scope, key, index)
  1806. VALUES (%s, %s, %s, %s, %s)
  1807. RETURNING id
  1808. """.format(table=self._table),
  1809. [name, self.env.user.id, scope, KEY_CRYPT_CONTEXT.hash(k), k[:INDEX_SIZE]])
  1810. ip = request.httprequest.environ['REMOTE_ADDR'] if request else 'n/a'
  1811. _logger.info("%s generated: scope: <%s> for '%s' (#%s) from %s",
  1812. self._description, scope, self.env.user.login, self.env.uid, ip)
  1813. return k
  1814. class APIKeyDescription(models.TransientModel):
  1815. _name = 'res.users.apikeys.description'
  1816. _description = 'API Key Description'
  1817. name = fields.Char("Description", required=True)
  1818. @check_identity
  1819. def make_key(self):
  1820. # only create keys for users who can delete their keys
  1821. self.check_access_make_key()
  1822. description = self.sudo()
  1823. k = self.env['res.users.apikeys']._generate(None, self.sudo().name)
  1824. description.unlink()
  1825. return {
  1826. 'type': 'ir.actions.act_window',
  1827. 'res_model': 'res.users.apikeys.show',
  1828. 'name': _('API Key Ready'),
  1829. 'views': [(False, 'form')],
  1830. 'target': 'new',
  1831. 'context': {
  1832. 'default_key': k,
  1833. }
  1834. }
  1835. def check_access_make_key(self):
  1836. if not self.user_has_groups('base.group_user'):
  1837. raise AccessError(_("Only internal users can create API keys"))
  1838. class APIKeyShow(models.AbstractModel):
  1839. _name = 'res.users.apikeys.show'
  1840. _description = 'Show API Key'
  1841. # the field 'id' is necessary for the onchange that returns the value of 'key'
  1842. id = fields.Id()
  1843. key = fields.Char(readonly=True)