Change Access Rights to all the User at a Time in Odoo - odoo

Now i have more than 300 Users in my system. For all of them by default Human Resources : Employee is there. Now, i want to remove that Access to all of them.
I tried to access from database, but no use.
How can i do that?

Try this,
from openerp import SUPERUSER_ID
#api.multi
def update_all_users_hr_employee_access(self):
category_id=self.pool.get('ir.module.category').search(cr,uid,[('name','=','Human Resources')],context=context)
group_ids=self.pool.get('res.groups').search(cr,uid,[('category_id','=',category_id[0]),('name','=','Employee')],context=context)
group_obj = self.pool.get('res.groups').browse(cr, uid,group_ids[0],context=context)
write_op = [(3, user.id) for user in group_obj.users if user.id!=SUPERUSER_ID]
group_obj.write(cr, uid, [group_obj.id], {'users': write_op}, context=context)
return True

Related

onchange method update virtual field's value

I am working on odoo v8 and I am trying to modify the access rights view in users form. I want to modify the form so that when I select a role for each module's category it will call the onchange method and update the checkboxes below (Technical Settings, Usability and Other). Currently changes made will only appear after I saved the form. But I want to make it update onfly for administrator to verify before saving into the database. But it seems when I return a dict from the onchange method the system is not aware the existence of the field (eg virtual fields like in_group_1, in_group_2 etc). Is there anyway to do this?
#api.v7
def check_acl(self, cr, uid, ids, my_field, context=None):
return {'value': {'in_group_1': True}}
You can check is administrator or not by this function
def check_user(self, uid=None):
if uid is None:
uid = request.uid
is_admin = request.registry['res.users']._is_admin(request.cr, uid, [uid])
if not is_admin:
raise openerp.exceptions.AccessError("Only administrators can upload a module")
and call this function with arguments like:
uid = request.session.authenticate(request.db, login, password)
self.check_user(uid)
and you can use this function in your onchange
#api.v7
def check_acl(self, cr, uid, ids, my_field, context=None):
if self.check_user(uid):
return {'value': {'in_group_1': True}}

Flask-Admin User Read/Write Settings Per Row

Say I have User and Subuser tables, where a User can have many Subusers. How can I only allow the logged in user (current_user) to view only the Subusers who are a part of the User? I.e. Subuser.user_id == current_user.id. I get I can do this by a filter, but this must be forced and not optional.
I'm using the SQLAlchemy backend if that helps!
It's pretty simple to override the queries that are produced by the backend in Flask Admin - you can do some quite complex things. Remember to override all of the below - especially get_one().
class BaseModelView(ModelView):
def is_accessible(self):
# if not authenticated we can't see this
if not login.current_user.is_authenticated():
return False
return True
def get_query(self):
return query.filter(self.model.parent_user_id.any(id=login.current_user.id))
def get_count_query(self):
# faster count query!
query = self.session.query(func.count('*')).select_from(self.model).filter(self.model.location_id == login.current_user.id)
return query
def get_one(self, id):
"""
Return a single model by its id.
:param id:
Model id
"""
result = self.session.query(self.model).get(iterdecode(id))
# if the users location does not exist and she is only allowed to edit locations they control
if login.current_user.id != result.parent_user_id:
app.logger.warn('You are not allowed to edit entries for this user.')
abort(404)
return result

Openerp Domain Filter Help on Tasks associated to Sales Team

I have been developing a lot of modules and implementing openerp for a wild. But I am stuck in a functional implementation.
I have installed the module crm_todo, it is for have tasks on crm, this module add "My Tasks" menu under Sales.
I need to create a new Menu with a Domain Filter called "Department Tasks" where will show all the task to all the members of an specific Sales Team. The task is assigned to User A; User A belongs to Sales Team A; Sales Team A has 2 more members. This new Menu have to list the task Assigned to User A to all the members of Sales Team A.
I was trying to do with field.function, but something is wrong. I am trying to apply the domain on act_window using the openerp Action Windows Menu and assigning it to the new Menu.
Specifying the login user sale team as domain parameter is not possible but there is another way in which we can achieve this. ie; in my view action i specify the domain as:
<field name="domain">[('user_id.default_section_id', 'in', user_sale_team())]</field>
where user_id is the responsible user of the task.
Now inherit read function of ir.actions.act_window and check if user_sale_team() is present in the domain of read result and replace this with the login user sale team id. This can be done as:
class ir_action_window(osv.osv):
_inherit = 'ir.actions.act_window'
def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
user_pool = self.pool.get('res.users')
obj_user = user_pool.browse(cr, uid, uid, context=context)
res = super(ir_action_window, self).read(cr, uid, ids, fields=fields, context=context, load=load)
if not isinstance(res, list):
res = [res]
sale_team_id = obj_user.default_section_id and obj_user.default_section_id.id or''
for r in res:
mystring = 'user_sale_team()'
if mystring in (r.get('domain', '[]') or ''):
r['domain'] = r['domain'].replace(mystring, str([sale_team_id]))
if isinstance(ids, (int, long)):
if res:
return res[0]
else:
return False
return res
ir_action_window()
This filters the result of the task to be displayed to each user based on his/her sale team.
Hope this helps.....

How to retrieve the current user in OpenERP module

I'm developing an OpenERP 7 module and I need to add a field that logs the user who created each record. How do I retrieve the current user object?
this kind of field is already available in openerp, as create_uid and write_uid.
In OpenERP Python code, functions generally take cr, the database pointer, and uid, the user id, as arguments. If all you need is the id of the current res.users object (for instance, to write into the one2many field), you can use uid as is. If you need to access the object (to see fields, etc.), something like:
current_user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
should work.

how to restrict number of records to create

Is there any way to restrict the number of records to be created for a single object or table
My requirement needs me to create only 3 contacts for my company. Is there any way to do so.
Thanks & Regards,
Atchuthan
You can use any one of the following method to restrict the number of records created.
1. Restrict from postgresql-- I dont know how to implement this
2. Restrict from python side.: Override your create method of your model and add check condition, if the limit is reached, then raise a warning.
For example, if you want to only to create max 5 users, then inherit res.users model,
class users(osv.osv):
_inherit="res.users"
def create(cr, uid, default, context=None):
res = super(user, self).create(cr, uid, default, context)
if len(self.search(cr, uid, [])) > 5:
raise osv_except('Error','User Limt exceeded')
return res
By default the limit is 80, however you can change this within this file:
web/addons/web/static/src/js/view_list.js
Full documentation on how to do it is at this website:
http://help.openerp.com/question/6627/how-set-limit-for-number-records-of-x2many/