Browse Source
* This eliminates the need to create an additional unsecure object in the database. * Using the API token also enables users with 2FA to authenticate against Odoo server * Added ACL for the Rest API view to allow access to only certain users * Removed Postman sample as the new methods has been simplified * Update the module info to reflect changes abovepull/345/head
9 changed files with 102 additions and 175 deletions
Binary file not shown.
@ -1,45 +0,0 @@ |
|||
# -*- coding:utf-8 -*- |
|||
############################################################################# |
|||
# |
|||
# Cybrosys Technologies Pvt. Ltd. |
|||
# |
|||
# Copyright (C) 2023-TODAY Cybrosys Technologies(<https://www.cybrosys.com>) |
|||
# Author: Cybrosys Techno Solutions(<https://www.cybrosys.com>) |
|||
# |
|||
# You can modify it under the terms of the GNU LESSER |
|||
# GENERAL PUBLIC LICENSE (LGPL v3), Version 3. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU LESSER GENERAL PUBLIC LICENSE (LGPL v3) for more details. |
|||
# |
|||
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE |
|||
# (LGPL v3) along with this program. |
|||
# If not, see <http://www.gnu.org/licenses/>. |
|||
# |
|||
############################################################################# |
|||
|
|||
import uuid |
|||
from odoo import fields, models |
|||
|
|||
|
|||
class UserLogin(models.Model): |
|||
"""This class is used to inherit users and add api key generation""" |
|||
_inherit = 'res.users' |
|||
|
|||
api_key = fields.Char(string="API Key", readonly=True, |
|||
help="Api key for connecting with the " |
|||
"Database.The key will be " |
|||
"generated when authenticating " |
|||
"rest api.") |
|||
|
|||
def generate_api(self, username): |
|||
"""This function is used to generate api-key for each user""" |
|||
users = self.env['res.users'].sudo().search([('login', '=', username)]) |
|||
if not users.api_key: |
|||
users.api_key = str(uuid.uuid4()) |
|||
key = users.api_key |
|||
else: |
|||
key = users.api_key |
|||
return key |
@ -0,0 +1,32 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<odoo> |
|||
<data noupdate="1"> |
|||
|
|||
<record model="ir.module.category" id="module_category_rest_api_odoo"> |
|||
<field name="name">REST API</field> |
|||
<field name="description">Helps you manage your REST API records.</field> |
|||
<field name="sequence">17</field> |
|||
</record> |
|||
|
|||
<record id="group_rest_api_odoo_manager" model="res.groups"> |
|||
<field name="name">Manager</field> |
|||
<field name="category_id" |
|||
ref="rest_api_odoo.module_category_rest_api_odoo" /> |
|||
<field name="users" eval="[(4, ref('base.user_root')), (4, ref('base.user_admin'))]" /> |
|||
</record> |
|||
|
|||
<record id="base.default_user" model="res.users"> |
|||
<field name="groups_id" |
|||
eval="[(4,ref('rest_api_odoo.group_rest_api_odoo_manager'))]" /> |
|||
</record> |
|||
|
|||
<record id="rest_api_odoo_rule_manager" model="ir.rule"> |
|||
<field name="name">All REST APIs</field> |
|||
<field name="model_id" ref="model_connection_api" /> |
|||
<field name="domain_force">[(1,'=',1)]</field> |
|||
<field name="groups" |
|||
eval="[(4, ref('rest_api_odoo.group_rest_api_odoo_manager'))]" /> |
|||
</record> |
|||
|
|||
</data> |
|||
</odoo> |
Before Width: | Height: | Size: 116 KiB After Width: | Height: | Size: 116 KiB |
@ -1,18 +0,0 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<odoo> |
|||
<!-- Inherited user view for Adding API key. --> |
|||
<record id="view_users_form" model="ir.ui.view"> |
|||
<field name="name">view.users.form.inherit.rest.api.odoo</field> |
|||
<field name="inherit_id" ref="base.view_users_form"/> |
|||
<field name="model">res.users</field> |
|||
<field name="arch" type="xml"> |
|||
<xpath expr="//page[@name='access_rights']" position="after"> |
|||
<page string="API" name="rest-api"> |
|||
<group> |
|||
<field name="api_key" groups="base.group_user"/> |
|||
</group> |
|||
</page> |
|||
</xpath> |
|||
</field> |
|||
</record> |
|||
</odoo> |
Loading…
Reference in new issue