Skip to content

Commit

Permalink
Create config file for package User #86
Browse files Browse the repository at this point in the history
  • Loading branch information
gghoantt committed Nov 16, 2015
1 parent 4a4c423 commit 4b30990
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/php-soft/laravel-users.svg)](https://travis-ci.org/php-soft/laravel-users)

> This module is use JWTAuth and ENTRUST libraries
>
>
> 1. https://github.com/tymondesigns/jwt-auth (JSON Web Token)
> 2. https://github.com/Zizaco/entrust (Role-based Permissions)
Expand Down Expand Up @@ -41,6 +41,15 @@ Next, also in the `app.php` config file, under the `aliases` array, you may want
]
```

You need to define namespace to your User model in the `phpsoft.user.php` config file, default with 'App\User'.

```php
'aliases' => [
// ...
'model' => 'App\User',
]
```

You will want to publish the config using the following command:

```sh
Expand Down Expand Up @@ -111,7 +120,7 @@ class User extends PhpSoftUser implements AuthenticatableContract, CanResetPassw

// ...
}
```
```

Remove middlewares in `app/Http/Kernel.php`

Expand Down Expand Up @@ -230,14 +239,14 @@ $user->can(['edit-user', 'create-post']); // true

### 3.3 Forgot password

To send mail forgot password,
To send mail forgot password,
- You need to add address and name of sender in `config\mail.php` as follows:

```php
'from' => ['address' => 'no-reply@example.com', 'name' => 'System'],
```

- You need to create email view:
- You need to create email view:
Create `password.blade.php` file in folder `resources\views\emails` with contents as follows:

```php
Expand Down Expand Up @@ -298,7 +307,7 @@ protected $routeMiddleware = [
Usage

```php
Route::group(['middleware'=>'routePermission'], function() {
Route::group(['middleware'=>'routePermission'], function() {
Route::post('/blog', function () {
//
});
Expand Down Expand Up @@ -358,7 +367,7 @@ class UserValidate implements Validator
{
/**
* Custom validator
*
*
* @return boolean
*/
public static function boot($request)
Expand All @@ -373,7 +382,7 @@ class UserValidate implements Validator

/**
* Declare rules
*
*
* @return array
*/
public static function rules()
Expand All @@ -386,4 +395,4 @@ class UserValidate implements Validator
}
}
```
Here, you will declare fields that you want to validate them in `rules()` function. And You can also custom validator fields that you want by declare them in `boot()` function.
Here, you will declare fields that you want to validate them in `rules()` function. And You can also custom validator fields that you want by declare them in `boot()` function.
43 changes: 33 additions & 10 deletions packages/Users/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

namespace PhpSoft\Users\Controllers;

use Input;
use Auth;
use Config;
use Input;
use JWTAuth;
use Validator;
use App\User as AppUser;
use Illuminate\Http\Request;
use PhpSoft\Users\Models\Role;
use PhpSoft\Users\Models\User;

class UserController extends Controller
{
private $className;

public function __construct()
{
$this->className = Config::get('phpsoft.user.model');
}

/**
* Display the specified resource.
*
Expand Down Expand Up @@ -58,7 +65,9 @@ public function store(Request $request)
]), 400);
}

$user = AppUser::create($request->all());
$className = $this->className;

$user = $className::create($request->all());

return response()->json(arrayView('phpsoft.users::user/read', [
'user' => $user
Expand Down Expand Up @@ -97,8 +106,10 @@ public function update(Request $request, $id = null)
]), 400);
}

$className = $this->className;

// check user
$user = $id ? AppUser::find($id) : Auth::user();
$user = $id ? $className::find($id) : Auth::user();

// Update profile
if (!$user) {
Expand All @@ -119,8 +130,10 @@ public function update(Request $request, $id = null)
*/
public function destroy($id)
{
$className = $this->className;

// get user by id
$user = AppUser::find($id);
$user = $className::find($id);

if (!$user) {
return response()->json(null, 404);
Expand All @@ -143,8 +156,10 @@ public function destroy($id)
*/
public function show($id)
{
$className = $this->className;

// get user by id
$user = AppUser::find($id);
$user = $className::find($id);

if (!$user) {
return response()->json(null, 404);
Expand All @@ -161,7 +176,9 @@ public function show($id)
*/
public function index(Request $request)
{
$users = AppUser::browse([
$className = $this->className;

$users = $className::browse([
'order' => [ Input::get('sort', 'id') => Input::get('direction', 'desc') ],
'limit' => ($limit = (int)Input::get('limit', 25)),
'cursor' => Input::get('cursor'),
Expand All @@ -182,7 +199,9 @@ public function index(Request $request)
*/
public function block($id)
{
$user = AppUser::find($id);
$className = $this->className;

$user = $className::find($id);

if (!$user) {
return response()->json(null, 404);
Expand All @@ -207,7 +226,9 @@ public function block($id)
*/
public function unblock($id)
{
$user = AppUser::find($id);
$className = $this->className;

$user = $className::find($id);

if (!$user) {
return response()->json(null, 404);
Expand All @@ -232,7 +253,9 @@ public function unblock($id)
*/
public function assignRole($id, Request $request)
{
$user = AppUser::find($id);
$className = $this->className;

$user = $className::find($id);

if (!$user) {
return response()->json(null, 404);
Expand Down
9 changes: 6 additions & 3 deletions packages/Users/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PhpSoft\Users\Models;

use App\User as AppUser;
use Config;
use Illuminate\Database\Eloquent\Model;
use PhpSoft\Users\Models\Role;

Expand Down Expand Up @@ -55,9 +55,10 @@ class User extends Model
*/
public static function create(array $attributes = [])
{
$className = Config::get('phpsoft.user.model');
$attributes['password'] = bcrypt($attributes['password']);

$user = new AppUser($attributes);
$user = new $className($attributes);
$user->save();

return $user;
Expand Down Expand Up @@ -98,7 +99,9 @@ public function changePassword($newPassword)
*/
public static function browse($options = [])
{
$find = new AppUser();
$className = Config::get('phpsoft.user.model');

$find = new $className;
$fillable = $find->fillable;

if (!empty($options['filters'])) {
Expand Down
1 change: 1 addition & 0 deletions packages/Users/Providers/UserServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function boot()
$this->publishes([
__DIR__ . '/../config/jwt.php' => config_path('jwt.php'),
__DIR__ . '/../config/entrust.php' => config_path('entrust.php'),
__DIR__ . '/../config/phpsoft.user.php' => config_path('phpsoft.user.php'),
]);

// Register commands
Expand Down

0 comments on commit 4b30990

Please sign in to comment.