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

69 lines
2.2 KiB

  1. # -*- coding: utf-8 -*-
  2. from odoo import models, fields, api
  3. from .. import random_tokens
  4. class Client(models.Model):
  5. _name = 'galicea_openid_connect.client'
  6. _description = 'OpenID Connect client'
  7. name = fields.Char(required=True)
  8. auth_redirect_uri = fields.Char('Redirect URI for user login')
  9. client_id = fields.Char(
  10. string='Client ID',
  11. required=True,
  12. readonly=True,
  13. index=True,
  14. default=lambda _: random_tokens.lower_case(16),
  15. )
  16. secret = fields.Char(
  17. string='Client secret',
  18. required=True,
  19. readonly=True,
  20. default=lambda _: random_tokens.alpha_numeric(32),
  21. groups='galicea_openid_connect.group_admin'
  22. )
  23. system_user_id = fields.Many2one(
  24. 'res.users',
  25. 'Artificial user representing the client in client credentials requests',
  26. readonly=True,
  27. required=True,
  28. ondelete='restrict'
  29. )
  30. allow_password_grant = fields.Boolean(
  31. string='Allow OAuth2 password grant',
  32. default=False,
  33. )
  34. @api.model
  35. def __system_user_name(self, client_name):
  36. return '{} - API system user'.format(client_name)
  37. @api.model
  38. def create(self, values):
  39. if 'name' in values:
  40. system_user = self.env['res.users'].create({
  41. 'name': self.__system_user_name(values['name']),
  42. 'login': random_tokens.lower_case(8),
  43. 'groups_id': [(4, self.env.ref('galicea_openid_connect.group_system_user').id)]
  44. })
  45. # Do not include in the "Pending invitations" list
  46. system_user.sudo(system_user.id)._update_last_login()
  47. values['system_user_id'] = system_user.id
  48. return super(Client, self).create(values)
  49. @api.multi
  50. def write(selfs, values):
  51. super(Client, selfs).write(values)
  52. if 'name' in values:
  53. selfs.mapped(lambda client: client.system_user_id).write({
  54. 'name': selfs.__system_user_name(values['name'])
  55. })
  56. return True
  57. @api.multi
  58. def unlink(selfs):
  59. users_to_unlink = selfs.mapped(lambda client: client.system_user_id)
  60. ret = super(Client, selfs).unlink()
  61. users_to_unlink.unlink()
  62. return ret