add Copyright and GPL license into the Header of Python files
[odoo/odoo.git] / bin / addons / base / res / res_user.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # $Id$
6 #
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
12 # Service Company
13 #
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.
18 #
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.
23 #
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.
27 #
28 ##############################################################################
29
30 from osv import fields,osv
31 import tools
32
33 class groups(osv.osv):
34         _name = "res.groups"
35         _columns = {
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'),
41         }
42         _sql_constraints = [
43                 ('name_uniq', 'unique (name)', 'The name of the group must be unique !')
44         ]
45
46         def write(self, cr, uid, ids, vals, context=None):
47                 if 'name' in vals:
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()
54                 return res
55
56         def create(self, cr, uid, vals, context=None):
57                 if 'name' in vals:
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)
62
63 groups()
64
65
66 class roles(osv.osv):
67         _name = "res.roles"
68         _columns = {
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')
72         }
73         _defaults = {
74         }
75         def check(self, cr, uid, ids, role_id):
76                 if role_id in ids:
77                         return True
78                 cr.execute('select parent_id from res_roles where id=%d', (role_id,))
79                 roles = cr.fetchone()[0]
80                 if roles:
81                         return self.check(cr, uid, ids, roles)
82                 return False
83 roles()
84
85 class users(osv.osv):
86         _name = "res.users"
87         _log_access = False
88         _columns = {
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)]"),
101         }
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)
105                 if not canwrite:
106                         for r in result:
107                                 if 'password' in r:
108                                         r['password'] = '********'
109                 return result
110
111         _sql_constraints = [
112                 ('login_key', 'UNIQUE (login)', 'You can not have two users with the same login !')
113         ]
114         _defaults = {
115                 'password' : lambda obj,cr,uid,context={} : '',
116                 'active' : lambda obj,cr,uid,context={} : True,
117         }
118         def company_get(self, cr, uid, uid2):
119                 company_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.id
120                 return company_id
121         company_get = tools.cache()(company_get)
122
123         def write(self, cr, uid, *args, **argv):
124                 res = super(users, self).write(cr, uid, *args, **argv)
125                 self.company_get()
126                 # Restart the cache on the company_get method
127                 self.pool.get('ir.rule').domain_get()
128                 return res
129
130         def unlink(self, cr, uid, ids):
131                 if 1 in 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)
134                 
135         def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=80):
136                 if not args:
137                         args=[]
138                 if not context:
139                         context={}
140                 ids = []
141                 if name:
142                         ids = self.search(cr, user, [('login','=',name)]+ args, limit=limit)
143                 if not ids:
144                         ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit)
145                 return self.name_get(cr, user, ids)
146                 
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)
151 users()
152
153 class groups2(osv.osv):
154         _inherit = 'res.groups'
155         _columns = {
156                 'users': fields.many2many('res.users', 'res_groups_users_rel', 'gid', 'uid', 'Users'),
157         }
158 groups2()
159