Skip to content

Commit

Permalink
[ADD] Add smile_website_login_as
Browse files Browse the repository at this point in the history
  • Loading branch information
Corentin POUHET-BRUNERIE committed Nov 7, 2016
1 parent 9e806d5 commit 7f2f84b
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 0 deletions.
22 changes: 22 additions & 0 deletions smile_website_login_as/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 Smile (<http://www.smile.fr>). All Rights Reserved
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import models
46 changes: 46 additions & 0 deletions smile_website_login_as/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 Smile (<http://www.smile.fr>). All Rights Reserved
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

{
"name": "Login as another user in website",
"version": "0.1",
"author": "Smile",
"website": 'http://www.smile.fr',
"license": 'AGPL-3',
"category": "Tools",
"description": """
Allows to login as another user in website
==========================================
Suggestions & Feedback to: [email protected] & [email protected]
""",
"depends": ['website'],
"data": [
"views/res_users_view.xml",
"views/webclient_templates.xml",
],
"demo": [],
"qweb": [
"static/src/xml/login_as.xml",
],
"installable": True,
"active": False,
}
23 changes: 23 additions & 0 deletions smile_website_login_as/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 Smile (<http://www.smile.fr>). All Rights Reserved
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import ir_http
import res_users
48 changes: 48 additions & 0 deletions smile_website_login_as/models/ir_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 Smile (<http://www.smile.fr>). All Rights Reserved
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import werkzeug

from odoo import models
from odoo.http import request


class Http(models.AbstractModel):
_inherit = 'ir.http'

@classmethod
def _website_enabled(cls):
try:
func, arguments = cls._find_handler()
return func.routing.get('website', False)
except werkzeug.exceptions.NotFound:
return True

@classmethod
def _dispatch(cls):
if cls._website_enabled():
if not request.uid and request.context.get('uid'):
user = request.env['res.users'].browse(request.context['uid'])
else:
user = request.env.user
if user:
request.uid = user.login_as_user_id.id or user.id
return super(Http, cls)._dispatch()
41 changes: 41 additions & 0 deletions smile_website_login_as/models/res_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 Smile (<http://www.smile.fr>). All Rights Reserved
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from odoo import api, fields, models


class ResUsers(models.Model):
_inherit = 'res.users'

login_as_user_id = fields.Many2one('res.users', 'Login as')

@api.multi
def login_as(self):
self.ensure_one()
return {
'type': 'ir.actions.act_url',
'url': '/',
'target': 'new',
}

@api.multi
def logout_as(self):
return self.write({'login_as_user_id': False})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions smile_website_login_as/static/src/js/login_as.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
odoo.define('login_as', function (require) {
'use strict';

var core = require('web.core');
var session = require('web.session');
var Model = require('web.DataModel');
var SystrayMenu = require('web.SystrayMenu');
var Widget = require('web.Widget');
var _t = core._t;

var LoginAs = Widget.extend({
template: 'LoginAs',
events: {
'click a#login_as': 'login_as',
},
login_as: function() {
var self = this;
new Model('ir.ui.view').call('get_view_id', ['smile_website_login_as.view_res_users_login_as_form']).then(function (view_id) {
self.do_action({
type: 'ir.actions.act_window',
name: _t('Login as'),
views: [[view_id, 'form']],
res_model: 'res.users',
res_id: session.uid,
target: 'new',
});
});
}
});

SystrayMenu.Items.push(LoginAs);

});
13 changes: 13 additions & 0 deletions smile_website_login_as/static/src/xml/login_as.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates>

<t t-name="LoginAs">
<li title="Login as">
<a href="#" id="login_as">
<i class="fa fa-globe"/>
</a>
</li>
</t>

</templates>

19 changes: 19 additions & 0 deletions smile_website_login_as/views/res_users_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record id="view_res_users_login_as_form" model="ir.ui.view">
<field name="name">res.users.login_as.form</field>
<field name="model">res.users</field>
<field name="arch" type="xml">
<form>
<sheet>
<field name="login_as_user_id" options="{'no_create': True, 'no_open': True}"/>
</sheet>
<footer>
<button name="login_as" type="object" string="Access to website" class="oe_highlight"/>
</footer>
</form>
</field>
</record>

</odoo>
12 changes: 12 additions & 0 deletions smile_website_login_as/views/webclient_templates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="0">

<template id="login_as" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/smile_website_login_as/static/src/js/login_as.js"></script>
</xpath>
</template>

</data>
</odoo>

0 comments on commit 7f2f84b

Please sign in to comment.