Skip to content

Commit

Permalink
Merge b73f881 into 5d170d4
Browse files Browse the repository at this point in the history
  • Loading branch information
vaibhavmule committed May 18, 2019
2 parents 5d170d4 + b73f881 commit 8de6487
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 110 deletions.
5 changes: 3 additions & 2 deletions app/http/middleware/LoadUserMiddleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
class LoadUserMiddleware:
"""Middleware class which loads the current user into the request."""

def __init__(self, request: Request):
def __init__(self, request: Request, auth: Auth):
"""Inject Any Dependencies From The Service Container.
Arguments:
Request {masonite.request.Request} -- The Masonite request object.
"""
self.request = request
self.auth = auth

def before(self):
"""Run This Middleware Before The Route Executes."""
Expand All @@ -30,4 +31,4 @@ def load_user(self):
Arguments:
request {masonite.request.Request} -- The Masonite request object.
"""
self.request.set_user(Auth(self.request).user())
self.request.set_user(self.auth.user())
Binary file modified masonite.db
Binary file not shown.
6 changes: 3 additions & 3 deletions masonite/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def singleton(self, name, class_obj):
obj = self.resolve(class_obj)
self.bind(name, obj)

def make(self, name):
def make(self, name, *arguments):
"""Retrieve a class from the container by key.
Arguments:
Expand All @@ -82,15 +82,15 @@ def make(self, name):
obj = self.providers[name]
self.fire_hook('make', name, obj)
if inspect.isclass(obj):
obj = self.resolve(obj)
obj = self.resolve(obj, *arguments)
return obj
elif name in self.swaps:
return self.swaps.get(name)
elif inspect.isclass(name):
obj = self._find_obj(name)
self.fire_hook('make', name, obj)
if inspect.isclass(obj):
obj = self.resolve(obj)
obj = self.resolve(obj, *arguments)
return obj

raise MissingContainerBindingNotFound(
Expand Down
6 changes: 4 additions & 2 deletions masonite/auth/Auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

import bcrypt

from masonite.app import App


class Auth:
"""This class will be used to authenticate users based on the config/auth.py file."""

_once = False

def __init__(self, request, auth_model=None):
def __init__(self, app: App, auth_model=None):
"""Auth constructor.
Arguments:
Expand All @@ -19,7 +21,7 @@ def __init__(self, request, auth_model=None):
Keyword Arguments:
auth_model {object} -- The model you want to authenticate with (default: {None})
"""
self.request = request
self.request = app.make('Request')

if auth_model:
self.auth_model = auth_model
Expand Down
2 changes: 2 additions & 0 deletions masonite/providers/AppProvider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""An AppProvider Service Provider."""

from config import application, middleware, storage
from masonite.auth import Auth
from masonite.autoload import Autoload
from masonite.commands import (AuthCommand, CommandCommand, ControllerCommand,
DownCommand, InfoCommand, InstallCommand,
Expand Down Expand Up @@ -38,6 +39,7 @@ def register(self):
self.app.bind('ExceptionDumpExceptionHandler', DumpHandler)
self.app.bind('RouteMiddleware', middleware.ROUTE_MIDDLEWARE)
self.app.bind('HttpMiddleware', middleware.HTTP_MIDDLEWARE)
self.app.bind('Auth', Auth)

# Insert Commands
self._load_commands()
Expand Down
15 changes: 8 additions & 7 deletions masonite/snippets/auth/controllers/ConfirmController.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ def __init__(self):
"""The ConfirmController Constructor."""
pass

def verify_show(self, request: Request, view: View):
def verify_show(self, request: Request, view: View, auth: Auth):
"""Show the Verify Email page for unverified users.
Arguments:
Request {masonite.request.request} -- The Masonite request class.
Request {masonite.view.view} -- The Masonite view class.
Request {masonite.auth.auth} -- The Masonite Auth class.
Returns:
[type] -- [description]
"""
return view.render('auth/verify', {'app': request.app().make('Application'), 'Auth': Auth(request)})
return view.render('auth/verify', {'app': request.app().make('Application'), 'Auth': auth})

def confirm_email(self, request: Request, view: View):
def confirm_email(self, request: Request, view: View, auth: Auth):
"""Confirm User email and show the correct response.
Arguments:
Request {masonite.request.request} -- The Masonite request class.
Request {masonite.view.view} -- The Masonite view class.
Request {masonite.auth.auth} -- The Masonite Auth class.
Returns:
[type] -- [description]
Expand All @@ -44,8 +46,7 @@ def confirm_email(self, request: Request, view: View):
if token is not None:
tokenParts = token.split("::")
if len(tokenParts) > 1:
id = tokenParts[0]
user = self.get_user(id)
user = self.get_user.find(1)

if user.verified_at is None:
timestamp = datetime.datetime.fromtimestamp(float(tokenParts[1]))
Expand All @@ -56,9 +57,9 @@ def confirm_email(self, request: Request, view: View):
user.verified_at = datetime.datetime.now()
user.save()

return view.render('auth/confirm', {'app': request.app().make('Application'), 'Auth': Auth(request)})
return view.render('auth/confirm', {'app': request.app().make('Application'), 'Auth': auth})

return view.render('auth/error', {'app': request.app().make('Application'), 'Auth': Auth(request)})
return view.render('auth/error', {'app': request.app().make('Application'), 'Auth': auth})

def get_user(self, id):
"""Get the user from the database.
Expand Down
6 changes: 3 additions & 3 deletions masonite/snippets/auth/controllers/HomeController.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class HomeController:
def __init__(self):
pass

def show(self, request: Request, view: View):
if not Auth(request).user():
def show(self, request: Request, view: View, auth: Auth):
if not auth.user():
request.redirect('/login')
return view.render('auth/home', {'app': request.app().make('Application'), 'Auth': Auth(request)})
return view.render('auth/home', {'app': request.app().make('Application'), 'Auth': auth})
15 changes: 9 additions & 6 deletions masonite/snippets/auth/controllers/LoginController.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,45 @@ def __init__(self):
"""LoginController Constructor."""
pass

def show(self, request: Request, view: View):
def show(self, request: Request, view: View, auth: Auth):
"""Show the login page.
Arguments:
request {masonite.request.Request} -- The Masonite request class.
view {masonite.view.View} -- The Masonite view class.
auth {masonite.auth.auth} -- The Masonite auth class.
Returns:
masonite.view.View -- Returns the Masonite view class.
"""
if request.user():
return request.redirect('/home')
return view.render('auth/login', {'app': request.app().make('Application'), 'Auth': Auth(request)})
return view.render('auth/login', {'app': request.app().make('Application'), 'Auth': auth})

def store(self, request: Request):
def store(self, request: Request, auth: Auth):
"""Login the user.
Arguments:
request {masonite.request.Request} -- The Masonite request class.
auth {masonite.auth.auth} -- The Masonite auth class.
Returns:
masonite.request.Request -- The Masonite request class.
"""
if Auth(request).login(request.input('email'), request.input('password')):
if auth.login(request.input('email'), request.input('password')):
return request.redirect('/home')

return request.redirect('/login')

def logout(self, request: Request):
def logout(self, request: Request, auth: Auth):
"""Log out the user.
Arguments:
request {masonite.request.Request} -- The Masonite request class.
auth {masonite.auth.auth} -- The Masonite auth class.
Returns:
masonite.request.Request -- The Masonite request class.
"""
Auth(request).logout()
auth.logout()
return request.redirect('/login')
8 changes: 4 additions & 4 deletions masonite/snippets/auth/controllers/PasswordController.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
class PasswordController:
"""Password Controller."""

def forget(self, view: View, request: Request):
return view.render('auth/forget', {'app': request.app().make('Application'), 'Auth': Auth(request)})
def forget(self, view: View, request: Request, auth: Auth):
return view.render('auth/forget', {'app': request.app().make('Application'), 'Auth': auth})

def reset(self, request: Request):
def reset(self, request: Request, auth: Auth):
token = request.param('token')
user = AUTH['model'].where('remember_token', token).first()
if user:
return view('auth/reset', {'token': token, 'app': request.app().make('Application'), 'Auth': Auth(request)})
return view('auth/reset', {'token': token, 'app': request.app().make('Application'), 'Auth': auth})

def send(self, request: Request, session: Session, mail: Mail):
email = request.input('email')
Expand Down
12 changes: 6 additions & 6 deletions masonite/snippets/auth/controllers/RegisterController.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""The RegisterController Module."""

from config import auth
from config import auth as auth_config
from masonite.auth import Auth
from masonite.helpers import password as bcrypt_password
from masonite.request import Request
Expand All @@ -16,7 +16,7 @@ def __init__(self):
"""The RegisterController Constructor."""
pass

def show(self, request: Request, view: View):
def show(self, request: Request, view: View, auth: Auth):
"""Show the registration page.
Arguments:
Expand All @@ -25,9 +25,9 @@ def show(self, request: Request, view: View):
Returns:
masonite.view.View -- The Masonite View class.
"""
return view.render('auth/register', {'app': request.app().make('Application'), 'Auth': Auth(request)})
return view.render('auth/register', {'app': request.app().make('Application'), 'Auth': auth})

def store(self, request: Request, mail_manager: MailManager):
def store(self, request: Request, mail_manager: MailManager, auth: Auth):
"""Register the user with the database.
Arguments:
Expand All @@ -36,7 +36,7 @@ def store(self, request: Request, mail_manager: MailManager):
Returns:
masonite.request.Request -- The Masonite request class.
"""
user = auth.AUTH['model'].create(
user = auth_config.AUTH['model'].create(
name=request.input('name'),
password=bcrypt_password(request.input('password')),
email=request.input('email'),
Expand All @@ -46,7 +46,7 @@ def store(self, request: Request, mail_manager: MailManager):
user.verify_email(mail_manager, request)

# Login the user
if Auth(request).login(request.input(auth.AUTH['model'].__auth__), request.input('password')):
if auth.login(request.input(auth_config.AUTH['model'].__auth__), request.input('password')):
# Redirect to the homepage
return request.redirect('/home')

Expand Down
4 changes: 2 additions & 2 deletions masonite/testing/DatabaseTestCase.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def registerFactory(self, model, callable_factory):
self.factory.register(model, callable_factory)

def setUpDatabase(self):
subprocess.run(['craft', 'migrate'])
subprocess.call(['craft', 'migrate'])

def tearDownDatabase(self):
subprocess.run(['craft', 'migrate:reset'])
subprocess.call(['craft', 'migrate:reset'])

def tearDown(self):
self.tearDownDatabase()
Loading

0 comments on commit 8de6487

Please sign in to comment.