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.3 KiB

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