Spaces:
Running
Running
Create main.py
Browse files- custom_auth/controllers/main.py +121 -0
custom_auth/controllers/main.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# custom_auth/controllers/main.py
|
| 2 |
+
|
| 3 |
+
from odoo import http, _
|
| 4 |
+
from odoo.http import request
|
| 5 |
+
from werkzeug.utils import redirect
|
| 6 |
+
|
| 7 |
+
class CustomAuthController(http.Controller):
|
| 8 |
+
|
| 9 |
+
# -------------------------
|
| 10 |
+
# SIGNUP PAGE
|
| 11 |
+
# -------------------------
|
| 12 |
+
@http.route(['/custom/signup'], type='http', auth='public', website=True)
|
| 13 |
+
def signup_page(self, **kw):
|
| 14 |
+
return request.render('custom_auth.signup_template', {})
|
| 15 |
+
|
| 16 |
+
@http.route(['/custom/signup/submit'], type='http', methods=['POST'], auth='public', website=True, csrf=True)
|
| 17 |
+
def signup_submit(self, **post):
|
| 18 |
+
name = post.get('name', '').strip()
|
| 19 |
+
login = post.get('login', '').strip()
|
| 20 |
+
password = post.get('password', '')
|
| 21 |
+
confirm = post.get('confirm_password', '')
|
| 22 |
+
|
| 23 |
+
errors = []
|
| 24 |
+
|
| 25 |
+
# Simple validation
|
| 26 |
+
if not name:
|
| 27 |
+
errors.append(_("Name is required"))
|
| 28 |
+
if not login:
|
| 29 |
+
errors.append(_("Email is required"))
|
| 30 |
+
if not password:
|
| 31 |
+
errors.append(_("Password is required"))
|
| 32 |
+
if password != confirm:
|
| 33 |
+
errors.append(_("Passwords do not match"))
|
| 34 |
+
|
| 35 |
+
# Email already exists?
|
| 36 |
+
existing_user = request.env['res.users'].sudo().search([('login', '=', login)], limit=1)
|
| 37 |
+
if existing_user:
|
| 38 |
+
errors.append(_("A user with this email already exists."))
|
| 39 |
+
|
| 40 |
+
if errors:
|
| 41 |
+
return request.render('custom_auth.signup_template', {
|
| 42 |
+
'errors': errors,
|
| 43 |
+
'values': post
|
| 44 |
+
})
|
| 45 |
+
|
| 46 |
+
# Create partner
|
| 47 |
+
Partner = request.env['res.partner'].sudo().create({
|
| 48 |
+
'name': name,
|
| 49 |
+
'email': login,
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
# Create user
|
| 53 |
+
try:
|
| 54 |
+
User = request.env['res.users'].sudo().create({
|
| 55 |
+
'name': name,
|
| 56 |
+
'login': login,
|
| 57 |
+
'email': login,
|
| 58 |
+
'partner_id': Partner.id,
|
| 59 |
+
'password': password,
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
# Add user to portal group
|
| 63 |
+
portal_group = request.env.ref('base.group_portal')
|
| 64 |
+
User.sudo().write({'groups_id': [(4, portal_group.id)]})
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
errors.append(_("Error creating user: %s") % e)
|
| 68 |
+
return request.render('custom_auth.signup_template', {
|
| 69 |
+
'errors': errors,
|
| 70 |
+
'values': post
|
| 71 |
+
})
|
| 72 |
+
|
| 73 |
+
# Auto login
|
| 74 |
+
try:
|
| 75 |
+
db = request.session.db
|
| 76 |
+
request.session.authenticate(db, login, password)
|
| 77 |
+
except:
|
| 78 |
+
return redirect('/custom/login?signup=ok')
|
| 79 |
+
|
| 80 |
+
return redirect('/')
|
| 81 |
+
|
| 82 |
+
# -------------------------
|
| 83 |
+
# LOGIN PAGE
|
| 84 |
+
# -------------------------
|
| 85 |
+
@http.route(['/custom/login'], type='http', auth='public', website=True)
|
| 86 |
+
def login_page(self, **kw):
|
| 87 |
+
message = None
|
| 88 |
+
if kw.get("signup") == "ok":
|
| 89 |
+
message = _("Signup successful. Please log in.")
|
| 90 |
+
return request.render("custom_auth.login_template", {'message': message})
|
| 91 |
+
|
| 92 |
+
@http.route(['/custom/login/submit'], type='http', methods=['POST'], auth='public', website=True, csrf=True)
|
| 93 |
+
def login_submit(self, **post):
|
| 94 |
+
login = post.get('login', '').strip()
|
| 95 |
+
password = post.get('password', '')
|
| 96 |
+
|
| 97 |
+
if not login or not password:
|
| 98 |
+
return request.render('custom_auth.login_template', {
|
| 99 |
+
'errors': [_("Enter both email and password.")],
|
| 100 |
+
'values': post
|
| 101 |
+
})
|
| 102 |
+
|
| 103 |
+
db = request.session.db
|
| 104 |
+
try:
|
| 105 |
+
request.session.authenticate(db, login, password)
|
| 106 |
+
except:
|
| 107 |
+
return request.render('custom_auth.login_template', {
|
| 108 |
+
'errors': [_("Incorrect email or password.")],
|
| 109 |
+
'values': post
|
| 110 |
+
})
|
| 111 |
+
|
| 112 |
+
next_url = post.get('redirect') or '/'
|
| 113 |
+
return redirect(next_url)
|
| 114 |
+
|
| 115 |
+
# -------------------------
|
| 116 |
+
# LOGOUT
|
| 117 |
+
# -------------------------
|
| 118 |
+
@http.route(['/custom/logout'], type='http', auth='user', website=True)
|
| 119 |
+
def logout(self, **kw):
|
| 120 |
+
request.session.logout(keep_db=True)
|
| 121 |
+
return redirect('/')
|