account,account_report,base,product,scrum,stock,kernel: fix name_search
[odoo/odoo.git] / bin / addons / base / res / res_user.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #                    Fabien Pinckaers <fp@tiny.Be>
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 from osv import fields,osv
30 import tools
31
32 class groups(osv.osv):
33         _name = "res.groups"
34         _columns = {
35                 'name': fields.char('Group Name', size=64, required=True),
36                 'model_access': fields.one2many('ir.model.access', 'group_id', 'Access Controls'),
37                 'rule_groups': fields.many2many('ir.rule.group', 'group_rule_group_rel', 'group_id', 'rule_group_id', 'Rules', domain="[('global', '<>', True)]"),
38         }
39         def write(self, cr, uid, *args, **argv):
40                 res = super(groups, self).write(cr, uid, *args, **argv)
41                 # Restart the cache on the company_get method
42                 self.pool.get('ir.rule').domain_get()
43                 return res
44 groups()
45
46
47 class roles(osv.osv):
48         _name = "res.roles"
49         _columns = {
50                 'name': fields.char('Role Name', size=64, required=True),
51                 'parent_id': fields.many2one('res.roles', 'Parent', select=True),
52                 'child_id': fields.one2many('res.roles', 'parent_id', 'Childs')
53         }
54         _defaults = {
55         }
56         def check(self, cr, uid, ids, role_id):
57                 if role_id in ids:
58                         return True
59                 cr.execute('select parent_id from res_roles where id=%d', (role_id,))
60                 roles = cr.fetchone()[0]
61                 if roles:
62                         return self.check(cr, uid, ids, roles)
63                 return False
64 roles()
65
66 class lang(osv.osv):
67         _name = "res.lang"
68         _columns = {
69                 'name': fields.char('Name', size=64, required=True),
70                 'code': fields.char('Code', size=5, required=True),
71                 'translatable': fields.boolean('Translatable'),
72                 'active': fields.boolean('Active'),
73                 'direction': fields.selection([('ltr', 'Left-to-right'), ('rtl', 'Right-to-left')], 'Direction',resuired=True),
74         }
75         _defaults = {
76                 'active': lambda *a: 1,
77                 'translatable': lambda *a: 0,
78                 'direction': lambda *a: 'ltr',
79         }
80 lang()
81
82 class users(osv.osv):
83         _name = "res.users"
84         _log_access = False
85         _columns = {
86                 'name': fields.char('Name', size=64, required=True, select=True),
87                 'login': fields.char('Login', size=64, required=True),
88                 'password': fields.char('Password', size=64, invisible=True),
89                 'signature': fields.text('Signature', size=64),
90                 'address_id': fields.many2one('res.partner.address', 'Address'),
91                 'active': fields.boolean('Active'),
92                 'action_id': fields.many2one('ir.actions.actions', 'Home Action'),
93                 'menu_id': fields.many2one('ir.actions.actions', 'Menu Action'),
94                 'groups_id': fields.many2many('res.groups', 'res_groups_users_rel', 'uid', 'gid', 'Groups'),
95                 'roles_id': fields.many2many('res.roles', 'res_roles_users_rel', 'uid', 'rid', 'Roles'),
96                 'company_id': fields.many2one('res.company', 'Company'),
97                 'rule_groups': fields.many2many('ir.rule.group', 'user_rule_group_rel', 'user_id', 'rule_group_id', 'Rules', domain="[('global', '<>', True)]"),
98         }
99         _sql_constraints = [
100                 ('login_key', 'UNIQUE (login)', 'You can not have two users with the same login !')
101         ]
102         _defaults = {
103                 'password' : lambda obj,cr,uid,context={} : '',
104                 'active' : lambda obj,cr,uid,context={} : True,
105         }
106         def company_get(self, cr, uid, uid2):
107                 company_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.id
108                 return company_id
109         company_get = tools.cache()(company_get)
110
111         def write(self, cr, uid, *args, **argv):
112                 res = super(users, self).write(cr, uid, *args, **argv)
113                 self.company_get()
114                 # Restart the cache on the company_get method
115                 self.pool.get('ir.rule').domain_get()
116                 return res
117
118         def unlink(self, cr, uid, ids):
119                 if 1 in ids:
120                         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, ...)')
121                 return super(users, self).unlink(cr, uid, ids)
122                 
123         def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=80):
124                 if not args:
125                         args=[]
126                 if not context:
127                         context={}
128                 ids = self.search(cr, user, [('login','=',name)]+ args, limit=limit)
129                 if not ids:
130                         ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit)
131                 return self.name_get(cr, user, ids)
132                 
133         def copy(self, cr, uid, id, default=None, context={}):
134                 login = self.read(cr, uid, [id], ['login'])[0]['login']
135                 default.update({'login': login+' (copy)'})
136                 return super(users, self).copy(cr, uid, id, default, context)
137 users()
138
139 class groups2(osv.osv):
140         _inherit = 'res.groups'
141         _columns = {
142                 'users': fields.many2many('res.users', 'res_groups_users_rel', 'gid', 'uid', 'Users'),
143         }
144 groups2()
145