Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Зуев Егор
/
wiki.dev
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Authored by
Dan Brown
2015-09-04 17:50:52 +0100
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Commit
3d18a04c3932f1724a67152e89619dda42a5e7cf
3d18a04c
1 parent
2dcc5105
Refactored Social auth into service, Made entity an abstract class
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
147 additions
and
77 deletions
.env.example
app/Entity.php
app/Http/Controllers/Auth/AuthController.php
app/Http/Controllers/BookController.php
app/Http/Controllers/UserController.php
app/Image.php
app/Repos/BookRepo.php
app/Services/SocialAuthService.php
.env.example
View file @
3d18a04
APP_ENV=local
APP_DEBUG=true
# Environment
APP_ENV=production
APP_DEBUG=false
APP_KEY=SomeRandomString
# Database details
DB_HOST=localhost
DB_DATABASE=
homestead
DB_USERNAME=
homestead
DB_PASSWORD=
secret
DB_DATABASE=
database_database
DB_USERNAME=
database_username
DB_PASSWORD=
database__user_password
# Cache and session
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
# Social Authentication
GITHUB_APP_ID=false
GITHUB_APP_SECRET=false
GOOGLE_APP_ID=false
GOOGLE_APP_SECRET=false
# URL for social login redirects, NO TRAILING SLASH
APP_URL=http://bookstack.dev
# Mail settings
MAIL_DRIVER=smtp
MAIL_HOST=
mailtrap.io
MAIL_PORT=
25
25
MAIL_HOST=
localhost
MAIL_PORT=
10
25
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
\ No newline at end of file
...
...
app/Entity.php
View file @
3d18a04
...
...
@@ -4,7 +4,7 @@ namespace Oxbow;
use
Illuminate\Database\Eloquent\Model
;
class
Entity
extends
Model
abstract
class
Entity
extends
Model
{
/**
* Relation for the user that created this entity.
...
...
@@ -86,4 +86,10 @@ class Entity extends Model
return
$search
->
get
();
}
/**
* Get the url for this item.
* @return string
*/
abstract
public
function
getUrl
();
}
...
...
app/Http/Controllers/Auth/AuthController.php
View file @
3d18a04
...
...
@@ -2,15 +2,13 @@
namespace
Oxbow\Http\Controllers\Auth
;
use
Oxbow\Exceptions\SocialDriverNotConfigured
;
use
Oxbow\Exceptions\UserNotFound
;
use
Oxbow\
Repos\UserRepo
;
use
Oxbow\
Services\SocialAuthService
;
use
Oxbow\User
;
use
Validator
;
use
Oxbow\Http\Controllers\Controller
;
use
Illuminate\Foundation\Auth\ThrottlesLogins
;
use
Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers
;
use
Laravel\Socialite\Contracts\Factory
as
Socialite
;
class
AuthController
extends
Controller
{
...
...
@@ -31,21 +29,16 @@ class AuthController extends Controller
protected
$redirectPath
=
'/'
;
protected
$redirectAfterLogout
=
'/login'
;
protected
$validSocialDrivers
=
[
'google'
,
'github'
];
protected
$socialite
;
protected
$userRepo
;
protected
$socialAuthService
;
/**
* Create a new authentication controller instance.
* @param Socialite $socialite
* @param UserRepo $userRepo
* @param SocialAuthService $socialAuthService
*/
public
function
__construct
(
Social
ite
$socialite
,
UserRepo
$userRepo
)
public
function
__construct
(
Social
AuthService
$socialAuthService
)
{
$this
->
middleware
(
'guest'
,
[
'except'
=>
'getLogout'
]);
$this
->
socialite
=
$socialite
;
$this
->
userRepo
=
$userRepo
;
$this
->
socialAuthService
=
$socialAuthService
;
}
/**
...
...
@@ -90,7 +83,7 @@ class AuthController extends Controller
return
view
(
'auth.authenticate'
);
}
$socialDrivers
=
$this
->
getActiveSocial
Drivers
();
$socialDrivers
=
$this
->
socialAuthService
->
getActive
Drivers
();
return
view
(
'auth.login'
,
[
'socialDrivers'
=>
$socialDrivers
]);
}
...
...
@@ -102,8 +95,7 @@ class AuthController extends Controller
*/
public
function
getSocialLogin
(
$socialDriver
)
{
$driver
=
$this
->
validateSocialDriver
(
$socialDriver
);
return
$this
->
socialite
->
driver
(
$driver
)
->
redirect
();
return
$this
->
socialAuthService
->
logIn
(
$socialDriver
);
}
/**
...
...
@@ -115,61 +107,9 @@ class AuthController extends Controller
*/
public
function
socialCallback
(
$socialDriver
)
{
$driver
=
$this
->
validateSocialDriver
(
$socialDriver
);
// Get user details from social driver
$socialUser
=
$this
->
socialite
->
driver
(
$driver
)
->
user
();
$user
=
$this
->
userRepo
->
getByEmail
(
$socialUser
->
getEmail
());
// Redirect if the email is not a current user.
if
(
$user
===
null
)
{
throw
new
UserNotFound
(
'A user with the email '
.
$socialUser
->
getEmail
()
.
' was not found.'
,
'/login'
);
}
$user
=
$this
->
socialAuthService
->
getUserFromCallback
(
$socialDriver
);
\Auth
::
login
(
$user
,
true
);
return
redirect
(
$this
->
redirectPath
);
}
/**
* Ensure the social driver is correct and supported.
*
* @param $socialDriver
* @return string
* @throws SocialDriverNotConfigured
*/
protected
function
validateSocialDriver
(
$socialDriver
)
{
$driver
=
trim
(
strtolower
(
$socialDriver
));
if
(
!
in_array
(
$driver
,
$this
->
validSocialDrivers
))
abort
(
404
,
'Social Driver Not Found'
);
if
(
!
$this
->
checkSocialDriverConfigured
(
$driver
))
throw
new
SocialDriverNotConfigured
;
return
$driver
;
}
/**
* Check a social driver has been configured correctly.
* @param $driver
* @return bool
*/
protected
function
checkSocialDriverConfigured
(
$driver
)
{
$upperName
=
strtoupper
(
$driver
);
$config
=
[
env
(
$upperName
.
'_APP_ID'
,
false
),
env
(
$upperName
.
'_APP_SECRET'
,
false
),
env
(
'APP_URL'
,
false
)];
return
(
!
in_array
(
false
,
$config
)
&&
!
in_array
(
null
,
$config
));
}
/**
* Gets the names of the active social drivers.
* @return array
*/
protected
function
getActiveSocialDrivers
()
{
$activeDrivers
=
[];
foreach
(
$this
->
validSocialDrivers
as
$driverName
)
{
if
(
$this
->
checkSocialDriverConfigured
(
$driverName
))
{
$activeDrivers
[
$driverName
]
=
true
;
}
}
return
$activeDrivers
;
}
}
...
...
app/Http/Controllers/BookController.php
View file @
3d18a04
...
...
@@ -144,6 +144,7 @@ class BookController extends Controller
$this
->
checkPermission
(
'book-delete'
);
$book
=
$this
->
bookRepo
->
getBySlug
(
$bookSlug
);
Activity
::
addMessage
(
'book_delete'
,
0
,
$book
->
name
);
Activity
::
removeEntity
(
$book
);
$this
->
bookRepo
->
destroyBySlug
(
$bookSlug
);
return
redirect
(
'/books'
);
}
...
...
app/Http/Controllers/UserController.php
View file @
3d18a04
...
...
@@ -100,7 +100,7 @@ class UserController extends Controller
});
$this
->
validate
(
$request
,
[
'name'
=>
'required'
,
'email'
=>
'required|email
'
,
'email'
=>
'required|email
|unique:users,email,'
.
$id
,
'password'
=>
'min:5'
,
'password-confirm'
=>
'same:password'
,
'role'
=>
'exists:roles,id'
...
...
app/Image.php
View file @
3d18a04
...
...
@@ -13,4 +13,12 @@ class Image extends Entity
return
storage_path
()
.
$this
->
url
;
}
/**
* Get the url for this item.
* @return string
*/
public
function
getUrl
()
{
return
public_path
()
.
$this
->
url
;
}
}
...
...
app/Repos/BookRepo.php
View file @
3d18a04
...
...
@@ -54,9 +54,11 @@ class BookRepo
{
$book
=
$this
->
getBySlug
(
$bookSlug
);
foreach
(
$book
->
pages
as
$page
)
{
\Activity
::
removeEntity
(
$page
);
$page
->
delete
();
}
foreach
(
$book
->
chapters
as
$chapter
)
{
\Activity
::
removeEntity
(
$chapter
);
$chapter
->
delete
();
}
$book
->
delete
();
...
...
app/Services/SocialAuthService.php
0 → 100644
View file @
3d18a04
<?php
namespace
Oxbow\Services
;
use
Laravel\Socialite\Contracts\Factory
as
Socialite
;
use
Oxbow\Exceptions\SocialDriverNotConfigured
;
use
Oxbow\Exceptions\UserNotFound
;
use
Oxbow\Repos\UserRepo
;
class
SocialAuthService
{
protected
$userRepo
;
protected
$socialite
;
protected
$validSocialDrivers
=
[
'google'
,
'github'
];
/**
* SocialAuthService constructor.
* @param $userRepo
* @param $socialite
*/
public
function
__construct
(
UserRepo
$userRepo
,
Socialite
$socialite
)
{
$this
->
userRepo
=
$userRepo
;
$this
->
socialite
=
$socialite
;
}
public
function
logIn
(
$socialDriver
)
{
$driver
=
$this
->
validateDriver
(
$socialDriver
);
return
$this
->
socialite
->
driver
(
$driver
)
->
redirect
();
}
/**
* Get a user from socialite after a oAuth callback.
*
* @param $socialDriver
* @return mixed
* @throws SocialDriverNotConfigured
* @throws UserNotFound
*/
public
function
getUserFromCallback
(
$socialDriver
)
{
$driver
=
$this
->
validateDriver
(
$socialDriver
);
// Get user details from social driver
$socialUser
=
$this
->
socialite
->
driver
(
$driver
)
->
user
();
$user
=
$this
->
userRepo
->
getByEmail
(
$socialUser
->
getEmail
());
// Redirect if the email is not a current user.
if
(
$user
===
null
)
{
throw
new
UserNotFound
(
'A user with the email '
.
$socialUser
->
getEmail
()
.
' was not found.'
,
'/login'
);
}
return
$user
;
}
/**
* Ensure the social driver is correct and supported.
*
* @param $socialDriver
* @return string
* @throws SocialDriverNotConfigured
*/
private
function
validateDriver
(
$socialDriver
)
{
$driver
=
trim
(
strtolower
(
$socialDriver
));
if
(
!
in_array
(
$driver
,
$this
->
validSocialDrivers
))
abort
(
404
,
'Social Driver Not Found'
);
if
(
!
$this
->
checklDriverConfigured
(
$driver
))
throw
new
SocialDriverNotConfigured
;
return
$driver
;
}
/**
* Check a social driver has been configured correctly.
* @param $driver
* @return bool
*/
private
function
checklDriverConfigured
(
$driver
)
{
$upperName
=
strtoupper
(
$driver
);
$config
=
[
env
(
$upperName
.
'_APP_ID'
,
false
),
env
(
$upperName
.
'_APP_SECRET'
,
false
),
env
(
'APP_URL'
,
false
)];
return
(
!
in_array
(
false
,
$config
)
&&
!
in_array
(
null
,
$config
));
}
/**
* Gets the names of the active social drivers.
* @return array
*/
public
function
getActiveDrivers
()
{
$activeDrivers
=
[];
foreach
(
$this
->
validSocialDrivers
as
$driverName
)
{
if
(
$this
->
checklDriverConfigured
(
$driverName
))
{
$activeDrivers
[
$driverName
]
=
true
;
}
}
return
$activeDrivers
;
}
}
\ No newline at end of file
Please
register
or
sign in
to post a comment