Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
新增:整合新北市 Open ID 認證
Browse files Browse the repository at this point in the history
  • Loading branch information
shengyou committed Jul 12, 2015
1 parent 1eb3fc2 commit 279d6df
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 9 deletions.
63 changes: 63 additions & 0 deletions app/Http/Controllers/Auth/OpenIdController.php
@@ -0,0 +1,63 @@
<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class OpenIdController extends Controller
{
public function redirect(Request $request)
{
$openId = new \LightOpenID($request->getHttpHost());

if(!$openId->mode) {
$openId->identity = config('openid.identity');
$openId->required = config('openid.required');
return redirect($openId->authUrl());
} else {
return redirect('login.index')->with('error', '登入失敗');
}
}

public function process(Request $request)
{
$openId = new \LightOpenID($request->getHttpHost());

if ($openId->mode == 'cancel') {
return redirect()->route('login.index')
->with('warning', '使用者取消授權');
}

if (!$openId->validate()) {
return redirect()->route('login.index')
->with('warning', '授權有誤');
}

$account = collect(array_values(explode('/', $openId->identity)))->last();

$attributes = $openId->getAttributes();
$name = $attributes['namePerson']; // 姓名
$email = $attributes['contact/email']; // 公務信箱

$user = User::firstOrNew([
'openid' => $account
]);

if (!$user->exists) {
$user->name = $name;
$user->email = $email;
$user->password = bcrypt(str_random(15));
$user->openid = $account;
$user->save();
}

\Auth::login($user);

return redirect()->route('posts.index')
->with('success', '登入成功');
}
}
5 changes: 4 additions & 1 deletion app/Http/routes.php
Expand Up @@ -52,4 +52,7 @@
Route::post('password/email', ['as' => 'forgetpassword.process', 'uses' => 'Auth\PasswordController@postEmail']);

Route::get('password/reset/{token}', ['as' => 'resetpassword.index' , 'uses' => 'Auth\PasswordController@getReset']);
Route::post('password/reset' , ['as' => 'resetpassword.process', 'uses' => 'Auth\PasswordController@postReset']);
Route::post('password/reset' , ['as' => 'resetpassword.process', 'uses' => 'Auth\PasswordController@postReset']);

Route::get('auth/openid' , ['as' => 'openid.process' , 'uses' => 'Auth\OpenIdController@process']);
Route::post('auth/openid', ['as' => 'openid.redirect', 'uses' => 'Auth\OpenIdController@redirect']);
2 changes: 1 addition & 1 deletion app/User.php
Expand Up @@ -24,7 +24,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
protected $fillable = ['name', 'email', 'password', 'openid'];

/**
* The attributes excluded from the model's JSON form.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -10,7 +10,8 @@
"guidovanbiemen/laravel-whoops": "^1.0",
"barryvdh/laravel-debugbar": "^2.0",
"doctrine/dbal": "^2.5",
"laravelcollective/html": "^5.1"
"laravelcollective/html": "^5.1",
"kounta/lightopenid": "^0.7"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
46 changes: 40 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions config/openid.php
@@ -0,0 +1,19 @@
<?php

return [

'identity' => 'http://openid.ntpc.edu.tw/',

'required' => [
'namePerson/friendly',
'contact/email',
'namePerson',
'birthDate',
'person/gender',
'contact/postalCode/home',
'contact/country/home',
'pref/language',
'pref/timezone'
],

];
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddOpenidInUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {

$table->string('openid')->after('password')->nullable()->index();

});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {

$table->dropColumn('openid');

});
}
}
4 changes: 4 additions & 0 deletions resources/views/auth/login.blade.php
Expand Up @@ -58,6 +58,10 @@
</div>
</div>
{!! Form::close() !!}

{!! Form::open(['route' => 'openid.redirect', 'method' => 'post']) !!}
{!! Form::submit('OpenID 登入', ['class' => 'btn btn-warning']) !!}
{!! Form::close() !!}
</div>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions resources/views/auth/register.blade.php
Expand Up @@ -63,6 +63,10 @@
</div>
</div>
{!! Form::close() !!}

{!! Form::open(['route' => 'openid.redirect', 'method' => 'post']) !!}
{!! Form::submit('OpenID 登入', ['class' => 'btn btn-warning']) !!}
{!! Form::close() !!}
</div>
</div>
</div>
Expand Down

0 comments on commit 279d6df

Please sign in to comment.