Skip to content

Commit

Permalink
Merge pull request #22 from lovata/develop
Browse files Browse the repository at this point in the history
Release version 1.5.0
  • Loading branch information
kharanenka authored Oct 28, 2018
2 parents 1facd72 + 2ef51f7 commit efa717b
Show file tree
Hide file tree
Showing 16 changed files with 815 additions and 65 deletions.
1 change: 1 addition & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function registerComponents()
return [
'\Lovata\Buddies\Components\Registration' => 'Registration',
'\Lovata\Buddies\Components\Login' => 'Login',
'\Lovata\Buddies\Components\SocialiteLogin' => 'SocialiteLogin',
'\Lovata\Buddies\Components\Logout' => 'Logout',
'\Lovata\Buddies\Components\ChangePassword' => 'ChangePassword',
'\Lovata\Buddies\Components\RestorePassword' => 'RestorePassword',
Expand Down
225 changes: 217 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,23 @@ The component is used to process the user registration form.
Required fields are: email, password, password_confirmation.
To send an ajax request, you must use the **Registration::onAjax** method.

**Example 1 (Submit form).**
**Example 1 (ajax submit)

```html
[Registration]
mode = "ajax"
redirect_on = 1
redirect_page = "registration_success"
activation = "activation_on"
force_login = 1
==
```

```javascript
$('form').request('Registration::onAjax');
```

**Example 2 (Submit form).**

```html

Expand Down Expand Up @@ -113,6 +129,9 @@ The component is used to activate the user in the activation mode by the link in

[ActivationPage]
slug = ":slug"
force_login = 1
redirect_on = 0
redirect_page = ""
==
```

Expand All @@ -127,7 +146,21 @@ slug = ":slug"
The component is used to process the user authorization form.
To send an ajax request, you must use the **Login::onAjax** method.

**Example 1 (Submit form)**
**Example 1 (ajax submit)

```html
[Login]
mode = "ajax"
redirect_on = 1
redirect_page = "index"
==
```

```javascript
$('form').request('Login::onAjax');
```

**Example 2 (Submit form)**

```html

Expand Down Expand Up @@ -169,6 +202,106 @@ The Login.getErrorMessage method returns an error message if the form was sent a
]
```

### onSocialiteLogin() method

Redirect to social login page

**Example (Send ajax request)**
```javascript

$.request('Login::onSocialiteLogin', {
data: {'driver': 'facebook'}
});
```

## Integration with [laravel/socialite](https://github.com/laravel/socialite) package

Documentation for Socialite can be found on the [Laravel website](https://laravel.com/docs/5.7/socialite) or [Socialite Providers](https://socialiteproviders.github.io/) site.

### Installation guide

You need to add [laravel/socialite](https://github.com/laravel/socialite) package and other socialite packages from [Socialite Providers](https://socialiteproviders.github.io/) list to your composer.json file.

```
{
"require": [
...
"laravel/socialite": "^3.1",
"socialiteproviders/vkontakte": "^4.0",
"socialiteproviders/instagram": "^3.0",
],
```

Execute below at the root of your project.
```
composer update
```
You can also install only packages and its dependencies without updating other packages by specifying the package.
```
composer require laravel/socialite
```

### Registration Socialite service provider

You need to register service provider class in your config/app.php file.

```php
'providers' => array_merge(include(base_path('modules/system/providers.php')), [
// 'Illuminate\Html\HtmlServiceProvider', // Example

'System\ServiceProvider',
\Laravel\Socialite\SocialiteServiceProvider::class,
]),


'aliases' => array_merge(include(base_path('modules/system/aliases.php')), [

// 'Str' => 'Illuminate\Support\Str', // Example
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
]),
```

### Integration with socialite package from [Socialite Providers](https://socialiteproviders.github.io/) list.

For example: integration with [socialiteproviders/instagram](https://socialiteproviders.github.io/providers/instagram.html) package.
1. Install package
```
composer require socialiteproviders/instagram
```
2. Remove Laravel\Socialite\SocialiteServiceProvider from your providers[] array in config\app.php if you have added it already.
Add \SocialiteProviders\Manager\ServiceProvider::class to your providers[] array in config\app.php.
```php
'providers' => array_merge(include(base_path('modules/system/providers.php')), [
// 'Illuminate\Html\HtmlServiceProvider', // Example

'System\ServiceProvider',
\SocialiteProviders\Manager\ServiceProvider::class,
]),
3. Add event listener
```php
\Event::listen(\SocialiteProviders\Manager\SocialiteWasCalled::class, 'SocialiteProviders\\Instagram\\InstagramExtendSocialite@handle');
```

### Usage example

1. Add facebook login button in your login page.
```javascript
$.request('Login::onSocialiteLogin', {data: {'driver': 'facebook'}});
```
2. Create a blank page and attach a component to it.
```html
title = "Facebook auth"
url = "/login/facebook"
is_hidden = 0

[SocialiteLogin]
code = "facebook"
redirect_success_page = "personal-data"
redirect_cancel_page = "login"
==
```


## Component "Logout"
**Component properties:**
- Mode (Submit form/Ajax form)
Expand All @@ -179,7 +312,21 @@ The Login.getErrorMessage method returns an error message if the form was sent a
The component is used for logout the user.
To send an ajax request, you must use the **Logout::onAjax** method.

**Example 1 (Submit form)**
**Example 1 (ajax submit)

```html
[Logout]
mode = "ajax"
redirect_on = 1
redirect_page = "index"
==
```

```javascript
$.request('Logout::onAjax');
```

**Example 2 (Submit form)**

```html

Expand All @@ -203,10 +350,23 @@ The component is used to process the user password change form.
Required fields are: old_password, password, password_confirmation.
To send an ajax request, you must use the **ChangePassword::onAjax** method.

**Example 1 (Submit form)**
**Example 1 (ajax submit)

```html
[ChangePassword]
mode = "ajax"
redirect_on = 1
redirect_page = "index"
check_old_password = 1
```

```javascript
$('form').request('ChangePassword::onAjax');
```

**Example 2 (Submit form)**

```html
[ChangePassword]
mode = "submit"
redirect_on = 1
Expand Down Expand Up @@ -253,10 +413,20 @@ The component is used to process the user password recovery form and send the em
Required fields are: email.
To send an ajax request, you must use the **RestorePassword::onAjax** method.

**Example 1 (Submit form)**
**Example 1 (ajax submit)

```html
[RestorePassword]
mode = "ajax"
```

```javascript
$('form').request('RestorePassword::onAjax');
```

**Example 2 (Submit form)**

```html
[RestorePassword]
mode = "submit"
==
Expand Down Expand Up @@ -316,10 +486,22 @@ The component is used to process the user password reset form.
Required fields are: password, password_confirmation.
To send an ajax request, you must use the **ResetPassword::onAjax** method.

**Example 1 (Submit form)**
**Example 1 (ajax submit)

```html
[ResetPassword]
mode = "ajax"
redirect_on = 1
redirect_page = "index"
```

```javascript
$('form').request('ResetPassword::onAjax');
```

**Example 2 (Submit form)**

```html
[ResetPassword]
mode = "submit"
redirect_on = 1
Expand Down Expand Up @@ -363,14 +545,27 @@ The ResetPassword.getErrorMessage method returns an error message if the form wa
The component is used to process the user data update form.
To send an ajax request, you must use the **UserPage::onAjax** method.

**Example 1 (Submit form).**
**Example 1 (ajax submit)

```html
[UserPage]
mode = "ajax"
redirect_on = 0
login_page = "login"
```

```javascript
$('form').request('UserPage::onAjax');
```

**Example 1 (Submit form).**

```html
[UserPage]
mode = "submit"
redirect_on = 1
redirect_page = "user_page"
login_page = "login"
==

{% set arError = UserPage.getErrorMessage %}
Expand Down Expand Up @@ -444,4 +639,18 @@ The UserItem class is extended from [ElementItem](https://github.com/lovata/oc-t
* (string) **phone**
* (array) **phone_list**
* (array) **property**
* (array) **avatar** - array with file list ([see](https://github.com/kharanenka/oc-data-file-model)).
* (array) **avatar** - array with file list ([see](https://github.com/kharanenka/oc-data-file-model)).

## Extending of login query

```php
User::extend(function ($obUser) {
$obUser->addDynamicMethod('scopeExtendLoginQuery', function ($query, $credential, $value) use ($obUser) {
if ($credential == 'email') {
$query = $query->orWhere('login', $value);
}

return $query;
});
});
```
3 changes: 3 additions & 0 deletions classes/AuthHelperManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ public function findUserByCredentials(array $arCredentials)
$arHashedCredentials = array_merge($arHashedCredentials, [$sCredential => $sValue]);
} else {
$obQuery = $obQuery->where($sCredential, '=', $sValue);
if ($obModel->methodExists('scopeExtendLoginQuery')) {
$obQuery = $obQuery->extendLoginQuery($sCredential, $sValue);
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions classes/item/UserItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @property string $middle_name
* @property string $phone
* @property array $phone_list
* @property array $socialite_token
* @property \System\Models\File $avatar
* @property array $property
*
Expand All @@ -30,4 +31,31 @@ class UserItem extends ElementItem

/** @var User */
protected $obElement = null;

/**
* Returns true, if user has socialite token with code == $sCode
* @param string $sCode
* @return bool
*/
public function hasSocialToken($sCode)
{
if (empty($this->socialite_token) || empty($sCode)) {
return false;
}

return in_array($sCode, $this->socialite_token);
}

/**
* Get additional element data for cache array
* @return array
*/
protected function getElementData()
{
$arResult = [
'socialite_token' => (array) $this->obElement->socialite_token()->lists('code'),
];

return $arResult;
}
}
Loading

0 comments on commit efa717b

Please sign in to comment.