diff --git a/src/Socialite/Controllers/Controller.php b/src/Socialite/Controllers/Controller.php index 63f019d..ebed8d7 100644 --- a/src/Socialite/Controllers/Controller.php +++ b/src/Socialite/Controllers/Controller.php @@ -3,6 +3,7 @@ namespace sinkcup\LaravelUiSocialite\Socialite\Controllers; use Illuminate\Foundation\Bus\DispatchesJobs; +use Illuminate\Http\Request; use Illuminate\Routing\Controller as BaseController; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; @@ -10,4 +11,18 @@ class Controller extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; + + public static function formatProviders($providers, Request $request) + { + // "WeChat Service Account Login" must be used in WeChat app. + if (!stripos($request->header('user-agent'), 'MicroMessenger')) { + if (in_array('wechat_service_account', $providers)) { + unset($providers[array_search('wechat_service_account', $providers)]); + } + } elseif (in_array('wechat_service_account', $providers) + && in_array('wechat_web', $providers)) { + unset($providers[array_search('wechat_web', $providers)]); + } + return $providers; + } } diff --git a/src/Socialite/Controllers/Settings/ProfileController.php b/src/Socialite/Controllers/Settings/ProfileController.php new file mode 100644 index 0000000..f03cf68 --- /dev/null +++ b/src/Socialite/Controllers/Settings/ProfileController.php @@ -0,0 +1,58 @@ +middleware('auth'); + } + + /** + * Show the form for editing the specified resource. + * + * @return \Illuminate\Http\Response + */ + public function edit() + { + $user = auth()->user(); + $social_login_providers = self::formatProviders(config('auth.social_login.providers'), request()); + $linked_providers = SocialAccount::where('user_id', $user->id)->select(['provider'])->pluck('provider')->all(); + return view('settings.profile', compact('user', 'social_login_providers', 'linked_providers') + + ['errors' => session('errors', new ViewErrorBag())] // HACK: only for test + ); + } + + /** + * Update the user's profile. + * + * @param Request $request + * @return \Illuminate\Http\Response + */ + public function update(Request $request) + { + $user = $request->user(); + $validated_data = Validator::make($request->all(), [ + 'email' => [ + Rule::unique('users')->ignore($user->id), + ], + 'name' => 'string|max:255', + ])->validate(); + $user->update($request->all()); + return redirect(route('profile.edit')); + } +} diff --git a/src/Socialite/Controllers/SocialiteLoginController.php b/src/Socialite/Controllers/SocialiteLoginController.php index 6f53017..46dcbbd 100644 --- a/src/Socialite/Controllers/SocialiteLoginController.php +++ b/src/Socialite/Controllers/SocialiteLoginController.php @@ -3,6 +3,7 @@ namespace sinkcup\LaravelUiSocialite\Socialite\Controllers; use Illuminate\Foundation\Auth\AuthenticatesUsers; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use Illuminate\Support\ViewErrorBag; use Laravel\Socialite\Facades\Socialite; @@ -22,23 +23,14 @@ class SocialiteLoginController extends Controller use AuthenticatesUsers; - /** + /** * Show the application's login form. * * @return \Illuminate\Http\Response */ public function showLoginForm() { - $providers = config('auth.social_login.providers'); - // "WeChat Service Account Login" must be used in WeChat app. - if (!stripos(request()->header('user-agent'), 'MicroMessenger')) { - if (in_array('wechat_service_account', $providers)) { - unset($providers[array_search('wechat_service_account', $providers)]); - } - } elseif (in_array('wechat_service_account', $providers) - && in_array('wechat_web', $providers)) { - unset($providers[array_search('wechat_web', $providers)]); - } + $providers = self::formatProviders(config('auth.social_login.providers'), request()); // "WeChat Web Login" will failed if you: // open URL in WeChat app and then "Scan QR Code in Image", or "Choose QR Code from Album" if (in_array('wechat_web', $providers)) { diff --git a/src/Socialite/stubs/controllers/settings/ProfileController.stub b/src/Socialite/stubs/controllers/settings/ProfileController.stub index 1f109f9..50dc03e 100644 --- a/src/Socialite/stubs/controllers/settings/ProfileController.stub +++ b/src/Socialite/stubs/controllers/settings/ProfileController.stub @@ -2,53 +2,8 @@ namespace {{namespace}}Http\Controllers\Settings; -use Illuminate\Http\Request; -use Illuminate\Support\Facades\Validator; -use Illuminate\Validation\Rule; -use sinkcup\LaravelUiSocialite\SocialAccount; -use {{namespace}}Http\Controllers\Controller; +use sinkcup\LaravelUiSocialite\Socialite\Controllers\Settings\ProfileController as PackageProfileController; -class ProfileController extends Controller +class ProfileController extends PackageProfileController { - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() - { - $this->middleware('auth'); - } - - /** - * Show the form for editing the specified resource. - * - * @return \Illuminate\Http\Response - */ - public function edit() - { - $user = auth()->user(); - $social_login_providers = config('auth.social_login.providers'); - $linked_providers = SocialAccount::where('user_id', $user->id)->select(['provider'])->pluck('provider')->all(); - return view('settings.profile', compact('user', 'social_login_providers', 'linked_providers')); - } - - /** - * Update the user's profile. - * - * @param Request $request - * @return \Illuminate\Http\Response - */ - public function update(Request $request) - { - $user = $request->user(); - $validated_data = Validator::make($request->all(), [ - 'email' => [ - Rule::unique('users')->ignore($user->id), - ], - 'name' => 'string|max:255', - ])->validate(); - $user->update($request->all()); - return redirect(route('profile.edit')); - } } diff --git a/src/Socialite/stubs/tests/Feature/ProfileControllerTest.stub b/src/Socialite/stubs/tests/Feature/ProfileControllerTest.stub deleted file mode 100644 index e163407..0000000 --- a/src/Socialite/stubs/tests/Feature/ProfileControllerTest.stub +++ /dev/null @@ -1,54 +0,0 @@ -create(); - $social_account = factory(SocialAccount::class)->create(['user_id' => $user->id]); - $response = $this->actingAs($user)->get('/settings/profile'); - - $response->assertViewIs('settings.profile'); - $response->assertViewHas('user', $user); - $response->assertViewHas('social_login_providers', config('auth.social_login.providers')); - $response->assertViewHas('linked_providers', [$social_account->provider]); - } - - public function testUpdate() - { - $user = factory(User::class)->create(); - $data = [ - 'email' => $this->faker->safeEmail, - 'name' => $this->faker->name, - ]; - $response = $this->actingAs($user)->put('/settings/profile', $data); - - $response->assertRedirect(route('profile.edit')); - $user->refresh(); - $this->assertEquals($data['email'], $user->email); - $this->assertEquals($data['name'], $user->name); - } - - public function testRedirectToLogin() - { - $response = $this->get('/home'); - - $response->assertStatus(302)->assertRedirect(route('login')); - } - - public function testJsonNotRedirectToLogin() - { - $response = $this->getJson('/home'); - - $response->assertStatus(401); - } -} diff --git a/src/SocialiteCommand.php b/src/SocialiteCommand.php index 388ab77..f556cb2 100644 --- a/src/SocialiteCommand.php +++ b/src/SocialiteCommand.php @@ -126,10 +126,6 @@ protected function exportBackend() __DIR__ . '/Socialite/stubs/tests/TestCase.stub', app_path() . '/../tests/TestCase.php' ); - file_put_contents( - app_path() . '/../tests/Feature/ProfileControllerTest.php', - $this->compileStub('tests/Feature/ProfileControllerTest.stub') - ); $web_routes = file_get_contents(base_path('routes/web.php')); foreach (explode("\n", file_get_contents(__DIR__ . '/Socialite/stubs/routes.stub')) as $line) { diff --git a/tests/Feature/ProfileControllerTest.php b/tests/Feature/ProfileControllerTest.php new file mode 100644 index 0000000..737f083 --- /dev/null +++ b/tests/Feature/ProfileControllerTest.php @@ -0,0 +1,71 @@ +create(); + $social_account = factory(SocialAccount::class)->create(['user_id' => $user->id]); + $response = $this->actingAs($user)->get('/settings/profile'); + + $response->assertViewIs('settings.profile'); + $response->assertViewHas('user', $user); + $response->assertViewHas('social_login_providers', config('auth.social_login.providers')); + $response->assertViewHas('linked_providers', [$social_account->provider]); + } + + public function testEditShouldHideWeChatWebWhenVisitFromWeChatApp() + { + $providers = ['github', 'wechat_service_account']; + $this->app['config']->set('auth.social_login.providers', array_merge($providers, ['wechat_web'])); + $user = factory(User::class)->create(); + $social_account = factory(SocialAccount::class)->create(['user_id' => $user->id]); + $response = $this->actingAs($user) + ->withHeader( + 'user-agent', + 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.5(0x17000523) NetType/WIFI Language/zh_CN' + )->get('/settings/profile'); + + $response->assertViewIs('settings.profile'); + $response->assertViewHas('user', $user); + $response->assertViewHas('social_login_providers', $providers); + $response->assertViewHas('linked_providers', [$social_account->provider]); + } + + public function testEditShouldHideWeChatServiceAccountWhenVisitFromWeb() + { + $providers = ['github', 'wechat_web']; + $this->app['config']->set('auth.social_login.providers', array_merge($providers, ['wechat_service_account'])); + $user = factory(User::class)->create(); + $response = $this->actingAs($user)->get('/settings/profile'); + + $response->assertViewIs('settings.profile'); + $response->assertViewHas('user', $user); + $response->assertViewHas('social_login_providers', $providers); + $response->assertViewHas('linked_providers', []); + } + + public function testUpdate() + { + $user = factory(User::class)->create(); + $data = [ + 'email' => $this->faker->safeEmail, + 'name' => $this->faker->name, + ]; + $response = $this->actingAs($user)->put('/settings/profile', $data); + + $response->assertRedirect(route('profile.edit')); + $user->refresh(); + $this->assertEquals($data['email'], $user->email); + $this->assertEquals($data['name'], $user->name); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index e3e253d..8d97930 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -35,9 +35,14 @@ protected function setUp(): void 'uses' => 'App\Http\Controllers\Auth\LoginController@handleProviderCallback', ]); Route::get('settings/profile', [ - 'uses' => 'App\Http\Controllers\ProfileController@edit', + 'uses' => 'App\Http\Controllers\Settings\ProfileController@edit', 'as' => 'profile.edit', ]); + Route::put('settings/profile', [ + 'uses' => 'App\Http\Controllers\Settings\ProfileController@update', + 'as' => 'profile.update', + ]); + Route::post('logout', 'Auth\LoginController@logout')->name('logout'); $laravel_path = __DIR__ . '/../vendor/orchestra/testbench-core/laravel'; @mkdir($laravel_path . '/app/Http/Controllers/', 0755, true); @mkdir($laravel_path . '/routes/');