1 ##############################################################################
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ##############################################################################
30 from osv import fields,osv
33 class groups(osv.osv):
36 'name': fields.char('Group Name', size=64, required=True),
37 'model_access': fields.one2many('ir.model.access', 'group_id', 'Access Controls'),
38 'rule_groups': fields.many2many('ir.rule.group', 'group_rule_group_rel',
39 'group_id', 'rule_group_id', 'Rules', domain="[('global', '<>', True)]"),
40 'menu_access': fields.many2many('ir.ui.menu', 'ir_ui_menu_group_rel', 'gid', 'menu_id', 'Access Menu'),
43 ('name_uniq', 'unique (name)', 'The name of the group must be unique !')
46 def write(self, cr, uid, ids, vals, context=None):
48 if vals['name'].startswith('-'):
49 raise osv.except_osv('Error',
50 'The name of the group can not start with "-"')
51 res = super(groups, self).write(cr, uid, ids, vals, context=context)
52 # Restart the cache on the company_get method
53 self.pool.get('ir.rule').domain_get()
56 def create(self, cr, uid, vals, context=None):
58 if vals['name'].startswith('-'):
59 raise osv.except_osv('Error',
60 'The name of the group can not start with "-"')
61 return super(groups, self).create(cr, uid, vals, context=context)
69 'name': fields.char('Role Name', size=64, required=True),
70 'parent_id': fields.many2one('res.roles', 'Parent', select=True),
71 'child_id': fields.one2many('res.roles', 'parent_id', 'Childs')
75 def check(self, cr, uid, ids, role_id):
78 cr.execute('select parent_id from res_roles where id=%d', (role_id,))
79 roles = cr.fetchone()[0]
81 return self.check(cr, uid, ids, roles)
89 'name': fields.char('Name', size=64, required=True, select=True),
90 'login': fields.char('Login', size=64, required=True),
91 'password': fields.char('Password', size=64, invisible=True),
92 'signature': fields.text('Signature', size=64),
93 'address_id': fields.many2one('res.partner.address', 'Address'),
94 'active': fields.boolean('Active'),
95 'action_id': fields.many2one('ir.actions.actions', 'Home Action'),
96 'menu_id': fields.many2one('ir.actions.actions', 'Menu Action'),
97 'groups_id': fields.many2many('res.groups', 'res_groups_users_rel', 'uid', 'gid', 'Groups'),
98 'roles_id': fields.many2many('res.roles', 'res_roles_users_rel', 'uid', 'rid', 'Roles'),
99 'company_id': fields.many2one('res.company', 'Company'),
100 'rule_groups': fields.many2many('ir.rule.group', 'user_rule_group_rel', 'user_id', 'rule_group_id', 'Rules', domain="[('global', '<>', True)]"),
102 def read(self,cr, uid, ids, fields=None, context=None, load='_classic_read'):
103 result = super(users, self).read(cr, uid, ids, fields, context, load)
104 canwrite = self.pool.get('ir.model.access').check(cr, uid, 'res.users', 'write', raise_exception=False)
108 r['password'] = '********'
112 ('login_key', 'UNIQUE (login)', 'You can not have two users with the same login !')
115 'password' : lambda obj,cr,uid,context={} : '',
116 'active' : lambda obj,cr,uid,context={} : True,
118 def company_get(self, cr, uid, uid2):
119 company_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.id
121 company_get = tools.cache()(company_get)
123 def write(self, cr, uid, *args, **argv):
124 res = super(users, self).write(cr, uid, *args, **argv)
126 # Restart the cache on the company_get method
127 self.pool.get('ir.rule').domain_get()
130 def unlink(self, cr, uid, ids):
132 raise osv.except_osv('Can not remove root user !', 'You can not remove the root user as it is used internally for resources created by Tiny ERP (updates, module installation, ...)')
133 return super(users, self).unlink(cr, uid, ids)
135 def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=80):
142 ids = self.search(cr, user, [('login','=',name)]+ args, limit=limit)
144 ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit)
145 return self.name_get(cr, user, ids)
147 def copy(self, cr, uid, id, default=None, context={}):
148 login = self.read(cr, uid, [id], ['login'])[0]['login']
149 default.update({'login': login+' (copy)'})
150 return super(users, self).copy(cr, uid, id, default, context)
153 class groups2(osv.osv):
154 _inherit = 'res.groups'
156 'users': fields.many2many('res.users', 'res_groups_users_rel', 'gid', 'uid', 'Users'),