2012-02-22 11:59:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2009-Today OpenERP SA (<http://www.openerp.com>)
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp.osv import fields, osv
|
2012-08-06 00:44:17 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp.tools.translate import _
|
2012-08-06 00:44:17 +00:00
|
|
|
|
2012-08-14 18:50:25 +00:00
|
|
|
class res_users(osv.Model):
|
2012-04-04 14:15:38 +00:00
|
|
|
""" Update of res.users class
|
2012-07-20 13:29:04 +00:00
|
|
|
- add a preference about sending emails about notifications
|
2012-04-04 14:15:38 +00:00
|
|
|
- make a new user follow itself
|
2012-07-20 13:29:04 +00:00
|
|
|
- add a welcome message
|
2012-04-04 14:15:38 +00:00
|
|
|
"""
|
2012-02-22 11:59:24 +00:00
|
|
|
_name = 'res.users'
|
2012-08-15 18:44:03 +00:00
|
|
|
_inherit = ['res.users']
|
2012-06-26 06:09:06 +00:00
|
|
|
_inherits = {'mail.alias': 'alias_id'}
|
2012-08-14 18:50:25 +00:00
|
|
|
|
2012-03-26 13:38:52 +00:00
|
|
|
_columns = {
|
2013-08-27 15:07:08 +00:00
|
|
|
'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="restrict", required=True,
|
2012-08-14 15:20:53 +00:00
|
|
|
help="Email address internally associated with this user. Incoming "\
|
|
|
|
"emails will appear in the user's notifications."),
|
2012-03-26 13:38:52 +00:00
|
|
|
}
|
2012-12-11 14:33:01 +00:00
|
|
|
|
2012-04-04 12:27:38 +00:00
|
|
|
_defaults = {
|
2012-12-11 14:33:01 +00:00
|
|
|
'alias_domain': False, # always hide alias during creation
|
2012-03-26 13:38:52 +00:00
|
|
|
}
|
2012-08-06 00:44:17 +00:00
|
|
|
|
2012-04-06 07:40:18 +00:00
|
|
|
def __init__(self, pool, cr):
|
2012-08-29 11:44:24 +00:00
|
|
|
""" Override of __init__ to add access rights on notification_email_send
|
2012-12-11 14:33:01 +00:00
|
|
|
and alias fields. Access rights are disabled by default, but allowed
|
|
|
|
on some specific fields defined in self.SELF_{READ/WRITE}ABLE_FIELDS.
|
2012-04-06 07:40:18 +00:00
|
|
|
"""
|
|
|
|
init_res = super(res_users, self).__init__(pool, cr)
|
|
|
|
# duplicate list to avoid modifying the original reference
|
|
|
|
self.SELF_WRITEABLE_FIELDS = list(self.SELF_WRITEABLE_FIELDS)
|
2012-08-29 11:44:24 +00:00
|
|
|
self.SELF_WRITEABLE_FIELDS.append('notification_email_send')
|
2012-12-11 14:33:01 +00:00
|
|
|
# duplicate list to avoid modifying the original reference
|
|
|
|
self.SELF_READABLE_FIELDS = list(self.SELF_READABLE_FIELDS)
|
|
|
|
self.SELF_READABLE_FIELDS.extend(['notification_email_send', 'alias_domain', 'alias_name'])
|
2012-04-06 07:40:18 +00:00
|
|
|
return init_res
|
2012-08-06 00:44:17 +00:00
|
|
|
|
2012-08-14 10:51:14 +00:00
|
|
|
def _auto_init(self, cr, context=None):
|
2012-09-10 08:31:35 +00:00
|
|
|
""" Installation hook: aliases, partner following themselves """
|
|
|
|
# create aliases for all users and avoid constraint errors
|
2013-04-17 07:55:46 +00:00
|
|
|
res = self.pool.get('mail.alias').migrate_to_alias(cr, self._name, self._table, super(res_users, self)._auto_init,
|
2012-08-24 14:39:44 +00:00
|
|
|
self._columns['alias_id'], 'login', alias_force_key='id', context=context)
|
2013-04-17 07:55:46 +00:00
|
|
|
return res
|
2012-08-14 10:51:14 +00:00
|
|
|
|
2012-02-22 11:59:24 +00:00
|
|
|
def create(self, cr, uid, data, context=None):
|
2012-06-26 06:09:06 +00:00
|
|
|
# create default alias same as the login
|
2012-09-13 18:52:31 +00:00
|
|
|
if not data.get('login', False):
|
2012-09-21 08:42:50 +00:00
|
|
|
raise osv.except_osv(_('Invalid Action!'), _('You may not create a user. To create new users, you should use the "Settings > Users" menu.'))
|
2012-09-13 18:52:31 +00:00
|
|
|
|
2012-07-05 14:17:06 +00:00
|
|
|
mail_alias = self.pool.get('mail.alias')
|
2012-08-06 00:44:17 +00:00
|
|
|
alias_id = mail_alias.create_unique_alias(cr, uid, {'alias_name': data['login']}, model_name=self._name, context=context)
|
|
|
|
data['alias_id'] = alias_id
|
2013-01-09 10:45:13 +00:00
|
|
|
data.pop('alias_name', None) # prevent errors during copy()
|
2012-08-16 10:18:48 +00:00
|
|
|
|
2013-01-09 10:45:13 +00:00
|
|
|
# create user
|
2012-08-20 16:08:00 +00:00
|
|
|
user_id = super(res_users, self).create(cr, uid, data, context=context)
|
2012-08-22 10:13:33 +00:00
|
|
|
user = self.browse(cr, uid, user_id, context=context)
|
2012-08-14 18:50:25 +00:00
|
|
|
# alias
|
|
|
|
mail_alias.write(cr, SUPERUSER_ID, [alias_id], {"alias_force_thread_id": user_id}, context)
|
2012-07-20 13:29:04 +00:00
|
|
|
# create a welcome message
|
2012-08-22 12:43:07 +00:00
|
|
|
self._create_welcome_message(cr, uid, user, context=context)
|
2012-02-22 11:59:24 +00:00
|
|
|
return user_id
|
2012-08-14 18:50:25 +00:00
|
|
|
|
2012-08-22 10:13:33 +00:00
|
|
|
def _create_welcome_message(self, cr, uid, user, context=None):
|
2012-11-15 08:34:00 +00:00
|
|
|
if not self.has_group(cr, uid, 'base.group_user'):
|
|
|
|
return False
|
|
|
|
company_name = user.company_id.name if user.company_id else ''
|
|
|
|
body = _('%s has joined the %s network.') % (user.name, company_name)
|
2012-08-22 11:34:39 +00:00
|
|
|
# TODO change SUPERUSER_ID into user.id but catch errors
|
|
|
|
return self.pool.get('res.partner').message_post(cr, SUPERUSER_ID, [user.partner_id.id],
|
2012-08-17 10:03:02 +00:00
|
|
|
body=body, context=context)
|
2012-08-14 18:50:25 +00:00
|
|
|
|
2012-06-26 06:09:06 +00:00
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
2012-08-06 00:44:17 +00:00
|
|
|
# User alias is sync'ed with login
|
2012-10-17 14:33:35 +00:00
|
|
|
if vals.get('login'):
|
|
|
|
vals['alias_name'] = vals['login']
|
2012-06-27 05:59:28 +00:00
|
|
|
return super(res_users, self).write(cr, uid, ids, vals, context=context)
|
2012-02-22 11:59:24 +00:00
|
|
|
|
2012-07-05 12:54:17 +00:00
|
|
|
def unlink(self, cr, uid, ids, context=None):
|
2012-07-05 14:17:06 +00:00
|
|
|
# Cascade-delete mail aliases as well, as they should not exist without the user.
|
2012-07-05 12:54:17 +00:00
|
|
|
alias_pool = self.pool.get('mail.alias')
|
2012-07-05 14:17:06 +00:00
|
|
|
alias_ids = [user.alias_id.id for user in self.browse(cr, uid, ids, context=context) if user.alias_id]
|
2012-07-05 12:54:17 +00:00
|
|
|
res = super(res_users, self).unlink(cr, uid, ids, context=context)
|
|
|
|
alias_pool.unlink(cr, uid, alias_ids, context=context)
|
|
|
|
return res
|
2012-08-14 18:50:25 +00:00
|
|
|
|
2012-12-21 14:40:14 +00:00
|
|
|
def _message_post_get_pid(self, cr, uid, thread_id, context=None):
|
2012-10-09 13:31:32 +00:00
|
|
|
assert thread_id, "res.users does not support posting global messages"
|
|
|
|
if context and 'thread_model' in context:
|
|
|
|
context['thread_model'] = 'res.partner'
|
|
|
|
if isinstance(thread_id, (list, tuple)):
|
|
|
|
thread_id = thread_id[0]
|
2013-01-03 13:04:57 +00:00
|
|
|
return self.browse(cr, SUPERUSER_ID, thread_id).partner_id.id
|
2012-12-21 14:40:14 +00:00
|
|
|
|
2012-10-19 11:34:08 +00:00
|
|
|
def message_post(self, cr, uid, thread_id, context=None, **kwargs):
|
|
|
|
""" Redirect the posting of message on res.users to the related partner.
|
|
|
|
This is done because when giving the context of Chatter on the
|
2012-12-21 14:52:47 +00:00
|
|
|
various mailboxes, we do not have access to the current partner_id. """
|
2012-12-21 14:40:14 +00:00
|
|
|
partner_id = self._message_post_get_pid(cr, uid, thread_id, context=context)
|
2012-10-19 11:34:08 +00:00
|
|
|
return self.pool.get('res.partner').message_post(cr, uid, partner_id, context=context, **kwargs)
|
|
|
|
|
2012-11-15 16:00:08 +00:00
|
|
|
def message_update(self, cr, uid, ids, msg_dict, update_vals=None, context=None):
|
2013-01-03 13:04:57 +00:00
|
|
|
for id in ids:
|
|
|
|
partner_id = self.browse(cr, SUPERUSER_ID, id).partner_id.id
|
|
|
|
self.pool.get('res.partner').message_update(cr, uid, [partner_id], msg_dict, update_vals=update_vals, context=context)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def message_subscribe(self, cr, uid, ids, partner_ids, subtype_ids=None, context=None):
|
|
|
|
for id in ids:
|
|
|
|
partner_id = self.browse(cr, SUPERUSER_ID, id).partner_id.id
|
|
|
|
self.pool.get('res.partner').message_subscribe(cr, uid, [partner_id], partner_ids, subtype_ids=subtype_ids, context=context)
|
|
|
|
return True
|
2012-11-15 16:00:08 +00:00
|
|
|
|
2013-02-22 10:13:02 +00:00
|
|
|
def message_get_partner_info_from_emails(self, cr, uid, emails, link_mail=False, context=None):
|
|
|
|
return self.pool.get('res.partner').message_get_partner_info_from_emails(cr, uid, emails, link_mail=link_mail, context=context)
|
|
|
|
|
|
|
|
def message_get_suggested_recipients(self, cr, uid, ids, context=None):
|
|
|
|
partner_ids = []
|
|
|
|
for id in ids:
|
|
|
|
partner_ids.append(self.browse(cr, SUPERUSER_ID, id).partner_id.id)
|
|
|
|
return self.pool.get('res.partner').message_get_suggested_recipients(cr, uid, partner_ids, context=context)
|
2013-01-15 11:00:58 +00:00
|
|
|
|
2013-03-07 07:52:16 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Compatibility methods: do not use
|
|
|
|
# TDE TODO: remove me in 8.0
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
|
|
|
def message_post_user_api(self, cr, uid, thread_id, context=None, **kwargs):
|
|
|
|
""" Redirect the posting of message on res.users to the related partner.
|
|
|
|
This is done because when giving the context of Chatter on the
|
|
|
|
various mailboxes, we do not have access to the current partner_id. """
|
|
|
|
partner_id = self._message_post_get_pid(cr, uid, thread_id, context=context)
|
|
|
|
return self.pool.get('res.partner').message_post_user_api(cr, uid, partner_id, context=context, **kwargs)
|
|
|
|
|
|
|
|
def message_create_partners_from_emails(self, cr, uid, emails, context=None):
|
|
|
|
return self.pool.get('res.partner').message_create_partners_from_emails(cr, uid, emails, context=context)
|
|
|
|
|
2012-09-03 15:20:25 +00:00
|
|
|
|
2012-08-14 18:50:25 +00:00
|
|
|
class res_users_mail_group(osv.Model):
|
2012-08-23 13:45:08 +00:00
|
|
|
""" Update of res.users class
|
|
|
|
- if adding groups to an user, check mail.groups linked to this user
|
|
|
|
group, and the user. This is done by overriding the write method.
|
2012-07-19 12:00:26 +00:00
|
|
|
"""
|
|
|
|
_name = 'res.users'
|
2012-08-14 16:48:17 +00:00
|
|
|
_inherit = ['res.users']
|
2012-07-19 12:00:26 +00:00
|
|
|
|
2012-08-22 08:36:08 +00:00
|
|
|
# FP Note: to improve, post processing may be better ?
|
2012-07-19 12:00:26 +00:00
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
|
|
|
write_res = super(res_users_mail_group, self).write(cr, uid, ids, vals, context=context)
|
|
|
|
if vals.get('groups_id'):
|
2012-07-19 12:34:39 +00:00
|
|
|
# form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
|
2012-07-19 12:00:26 +00:00
|
|
|
user_group_ids = [command[1] for command in vals['groups_id'] if command[0] == 4]
|
2012-07-19 12:34:39 +00:00
|
|
|
user_group_ids += [id for command in vals['groups_id'] if command[0] == 6 for id in command[2]]
|
2012-07-19 12:00:26 +00:00
|
|
|
mail_group_obj = self.pool.get('mail.group')
|
|
|
|
mail_group_ids = mail_group_obj.search(cr, uid, [('group_ids', 'in', user_group_ids)], context=context)
|
2012-08-16 15:48:23 +00:00
|
|
|
mail_group_obj.message_subscribe_users(cr, uid, mail_group_ids, ids, context=context)
|
2012-07-19 12:00:26 +00:00
|
|
|
return write_res
|
|
|
|
|
2012-08-14 18:50:25 +00:00
|
|
|
class res_groups_mail_group(osv.Model):
|
2012-07-19 12:00:26 +00:00
|
|
|
""" Update of res.groups class
|
2012-08-23 13:45:08 +00:00
|
|
|
- if adding users from a group, check mail.groups linked to this user
|
|
|
|
group and subscribe them. This is done by overriding the write method.
|
2012-07-19 12:00:26 +00:00
|
|
|
"""
|
|
|
|
_name = 'res.groups'
|
|
|
|
_inherit = 'res.groups'
|
|
|
|
|
2012-08-22 08:36:08 +00:00
|
|
|
# FP Note: to improve, post processeing, after the super may be better
|
2012-07-19 12:00:26 +00:00
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
2012-08-23 13:45:08 +00:00
|
|
|
write_res = super(res_groups_mail_group, self).write(cr, uid, ids, vals, context=context)
|
2012-07-19 12:00:26 +00:00
|
|
|
if vals.get('users'):
|
2012-07-19 12:34:39 +00:00
|
|
|
# form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
|
|
|
|
user_ids = [command[1] for command in vals['users'] if command[0] == 4]
|
|
|
|
user_ids += [id for command in vals['users'] if command[0] == 6 for id in command[2]]
|
2012-07-19 12:00:26 +00:00
|
|
|
mail_group_obj = self.pool.get('mail.group')
|
|
|
|
mail_group_ids = mail_group_obj.search(cr, uid, [('group_ids', 'in', ids)], context=context)
|
2012-08-16 15:48:23 +00:00
|
|
|
mail_group_obj.message_subscribe_users(cr, uid, mail_group_ids, user_ids, context=context)
|
2012-08-23 13:45:08 +00:00
|
|
|
return write_res
|