Skip to Content
Menu
This question has been flagged

i created a custom module and now i want to give a security rule that any users who logged in can can edit their own records

Thanks in advance

my code

from odoo import fields, models, api
from odoo.exceptions import UserError





class Subject(models.Model):
_name = 'std.subject'
_description = 'Subject'

name = fields.Char(string='Subject')
student_id = fields.Many2many('std.record', string='students')


class StudentDetails(models.Model):
_name = 'std.record'
_description = 'Student'

name = fields.Char(string='Name')
middle_name = fields.Char(string='Middle Name')
last_name = fields.Char(string='Last Name')
roll_no = fields.Integer(string='Roll Number')
std_dob = fields.Date(string='Date of Birth')
image = fields.Binary(string='Image')
introduction = fields.Text(string="Introduction")
std_class = fields.Many2one(comodel_name='std.class', string='Student Class')
subj = fields.Many2many(comodel_name='std.subject', string='Subject')
user_id = fields.Many2one(comodel_name='res.users',string='User')

_sql_constraints = [
('unique_roll_number', 'unique(roll_no)', 'This roll number is already used on another student.'), ]

@api.constrains('std_dob')
def check_student_dob(self):
if self.std_dob and self.std_dob >= fields.Date.today():
raise UserError(('DOB should be less than Today\'s Date.'))


class Class(models.Model):
_name = 'std.class'
_description = 'Class'

name = fields.Char(string='Class')
stdnt_class = fields.One2many(
comodel_name='std.record',
inverse_name='std_class',
string='class',
required=False)


Avatar
Discard
Best Answer

Hello alfik,

Please find below answer it may help you,

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Discard

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="base.module_category_student" model="ir.module.category">
<field name="description">Helps you handle your student details.</field>
<field name="sequence">1</field>
</record>

<!-- Groups -->
<record id="group_student_user" model="res.groups">
<field name="name">User: Own Documents Only</field>
<field name="category_id" ref="base.module_category_student"/>
<field name="implied_ids" eval="[(4, ref('base.group_user'))]"/>
<field name="comment">the user will have access to his own data.</field>
</record>

<record id="group_student_manager" model="res.groups">
<field name="name">Administrator</field>
<field name="comment">the user will have an access to the student configuration as well as all user recode.</field>
<field name="category_id" ref="base.module_category_student"/>
<field name="implied_ids" eval="[(4, ref('group_student_user'))]"/>
<field name="users" eval="[(4, ref('base.user_root')), (4, ref('base.user_admin'))]"/>
</record>
<!-- Recode Rules -->
<record id="studunt_rule_all_recode" model="ir.rule">
<field name="name">All Student</field>
<field ref="model_std_record" name="model_id"/>
<field name="domain_force">[(1,'=',1)]</field>
<field name="groups" eval="[(4, ref('YOURE_MODULE_NAME.group_student_manager'))]"/>
</record>

<record model="ir.rule" id="student_own_recode">
<field name="name">Users can only access their own recode</field>
<field name="model_id" ref="model_std_record"/>
<field name="domain_force">[('user_id', '=', user.id)]</field>
</record>
</odoo>

Author

Thanks a bunch Jainesh Shah

Related Posts Replies Views Activity
2
Jul 24
6077
0
Jan 24
348
2
May 23
5978
0
Mar 22
101
2
Feb 22
2555