定制 zizaco/confide-mongo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zizaco/confide-mongo

Composer 安装命令:

composer require zizaco/confide-mongo

包简介

Confide Mongo is a authentication solution for Laravel 4 that uses mongolid-laravel

README 文档

README

Confide Poster

Build Status ProjectStatus

Confide is a authentication solution for Laravel4 using MongoLid made to eliminate repetitive tasks involving the management of users: Account creation, login, logout, confirmation by e-mail, password reset, etc.

Confide aims to be simple to use, quick to configure and flexible.

Note: If you are NOT using MongoDB check Confide.

Features

Current:

  • Account confirmation (through confirmation link).
  • Password reset (sending email with a change password link).
  • Easily render forms for login, signup and password reset.
  • Generate customizable routes for login, signup, password reset, confirmation, etc.
  • Generate a customizable controller that handles the basic user account actions.
  • Contains a set of methods to help basic user features.
  • Integrated with the Laravel Auth component/configs.
  • Field/model validation.
  • Login throttling.
  • Redirecting to previous route after authentication.

If you are looking for user roles and permissions see Entrust

Planned:

  • Captcha in user signup and password reset.
  • General improvements.

Quick start

Required setup

In the require key of composer.json file add the following

"zizaco/confide-mongo": "dev-master"

Run the Composer update comand

$ composer update

In your config/app.php add 'Zizaco\ConfideMongo\ConfideMongoServiceProvider' to the end of the $providers array

'providers' => array(

    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    ...
    'Zizaco\ConfideMongo\ConfideMongoServiceProvider',

),

At the end of config/app.php add 'Confide' => 'Zizaco\Confide\ConfideFacade' to the $aliases array

'aliases' => array(

    'App'        => 'Illuminate\Support\Facades\App',
    'Artisan'    => 'Illuminate\Support\Facades\Artisan',
    ...
    'Confide'    => 'Zizaco\Confide\ConfideFacade',

),

Configuration

Set the driver to "mongoLid" in config/auth.php as stated in MongoLid Authentication:

    ...

    'driver' => 'mongoLid',
    
    ...

This values contained in config/auth.php will be used by Confide Mongo to generate the controllers and routes.

Set the address and name from the from array in config/mail.php. Those will be used to send account confirmation and password reset emails to the users.

User model

Change your User model in app/models/User.php to:

<?php

use Zizaco\ConfideMongo\ConfideMongoUser;

class User extends ConfideMongoUser {

}

ConfideMongoUser class will take care of some behaviors of the user model.

Dump the default acessors

Least, you can dump a default controller and the default routes for Confide.

$ php artisan confide:controller
$ php artisan confide:routes

Don't forget to dump composer autoload

$ composer dump-autoload

And you are ready to go. Access http://yourapp/user/create to create your first user. Check the app/routes.php to see the available routes.

Usage in detail

Basic setup:

  1. Mongo database connection in config/database.php running properly.
  2. Set the auth driver to "mongoLid" in config/auth.php.
  3. Check and correct the model and collection names in config/auth.php. They will be used by Confide all the time.
  4. from configuration in config/mail.php.

Configuration:

  1. ConfideMongoServiceProvider and ConfideFacade entry in config/app.php 'providers' and 'aliases' respectively.
  2. User model (with the same name as in config/auth.php) should extend ConfideMongoUser class. This will cause to methods like resetPassword(), confirm() and a overloaded save() to be available.

Optional steps:

  1. Use Confide facade to dump login and signup forms easly with makeLoginForm() and makeSignupForm(). You can render the forms within your views by doing {{ Confide::makeLoginForm()->render() }}.
  2. Generate a controller with the template contained in Confide throught the artisan command $ php artisan confide:controller. If a controller with the same name exists it will NOT be overwritten.
  3. Generate routes matching the controller template throught the artisan command $ php artisan confide:routes. Your routes.php will NOT be overwritten.

Advanced

Using custom collection / model name

You can change the model name that will be authenticated in the config/auth.php file. Confide uses the values present in that configuration file.

To change the controller name when dumping the default controller template you can use the --name option.

$ php artisan confide:controller --name Employee

Will result in EmployeeController

Then, when dumping the routes, you should use the --controller option to match the existing controller.

$ php artisan confide:routes --controller Employee

Using custom form or emails

First, publish the config files:

$ php artisan config:publish zizaco/confide

Then edit the view names in app/config/packages/zizaco/confide/config.php.

Update an User

To update an user already in the database you'll want to either pass in an different rule set or use the amend function.

$user = new User;
$user->username = 'newUserName';

// Save
$user->save($this->getUpdateRules());

Validate model fields

To change the validation rules of the User model you can take a look at Laravel 4 Validations. For example:

<?php

use Zizaco\ConfideMongo\ConfideMongoUser;

class User extends ConfideMongoUser {

    /**
     * Validation rules
     */
    public static $rules = array(
        'email' => 'required|email',
        'password' => 'required|between:4,11|confirmed',
    );

}

Feel free to add more fields to your collection and to the validation array. Then you should build your own sign-up form with the additional fields.

Passing additional information to the make methods

If you want to pass additional parameters to the forms, you can use an alternate syntax to achieve this.

Instead of using the make method:

Confide::makeResetPasswordForm( $token ):

You would use:

View::make(Config::get('confide::reset_password_form'))
    ->with('token', $token);

It produces the same output, but you would be able to add more inputs using 'with' just like any other view.

RESTful controller

If you want to generate a RESTful controller you can use the aditional --restful or -r option.

$ php artisan confide:controller --restful

Will result in a RESTful controller

Then, when dumping the routes, you should use the --restful option to match the existing controller.

$ php artisan confide:routes --restful

User roles and permissions

In order not to bloat Confide with not related features, the role and permission was developed as another package: Entrust. This package couples very well with Confide.

See Entrust

Note: Entrust is not yet available for MongoLid / MongoDB

Redirecting to previous route after login

When defining your filter you should set the 'loginRedirect' session variable. For example:

// filters.php

Route::filter('auth', function()
{
    if ( Auth::guest() ) // If the user is not logged in
    {
        // Set the loginRedirect session variable
        Session::put( 'loginRedirect', Request::url() );

        // Redirect back to user login
        return Redirect::to( 'user/login' );
    }
});

// Only authenticated users will be able to access routes that begins with
// 'admin'. Ex: 'admin/posts', 'admin/categories'.
Route::when('admin*', 'auth'); 

or, if you are using Entrust ;)

// filters.php

Entrust::routeNeedsRole( 'admin*', 'Admin', function(){
    Session::put( 'loginRedirect', Request::url() );
    return Redirect::to( 'user/login' );
} );

Validating a route

If you want to validate whether a route exists, the Confide::checkAction function is what you are looking for.

Currently it is used within the views to determine Non-RESTful vs RESTful routes.

Troubleshooting

"'confirmation_code' required" when creating a user

If you overwrite the save() method in your model, make sure to call parent::save():

public function save( $forced = false ){

    parent::save( $forced) // Don't forget this

    // Your stuff
}

Confirmation link is not sent when user signup

If you overwrite the afterSave() method in your model, make sure to call parent::afterSave()

Users are able to login without confirming account

If you want only confirmed users to login, in your UserController, instead of simply calling logAttempt( $input ), call logAttempt( $input, true ). The second parameter stands for "confirmed_only".

License

Confide is free software distributed under the terms of the MIT license

Aditional information

Any questions, feel free to contact me or ask here

Any issues, please report here

zizaco/confide-mongo 适用场景与选型建议

zizaco/confide-mongo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.61k 次下载、GitHub Stars 达 32, 最近一次更新时间为 2013 年 06 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「auth」 「laravel」 「illuminate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 zizaco/confide-mongo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zizaco/confide-mongo 我们能提供哪些服务?
定制开发 / 二次开发

基于 zizaco/confide-mongo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 18.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 34
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 32
  • Watchers: 8
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-06-02