未加星标

Laravel Sentry and UUID

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
Update October 10 10:07

Have hit a few issues with this model but still working okay.

To start this trait does some of the work to manage incoming POST data.

<php namespace BehatEditor\Helpers; use Rhumsaa\Uuid\Uuid; trait UuidHelper { protected $uuid; protected $saved; public function getUuid() { return $this->uuid; } public function setUuid($uuid = null) { if($uuid === null) { $uuid = $this->generateNewId()->toString(); } $this->uuid = $uuid; return $this; } /** * Get a new version 4 (random) UUID. * * @return \Rhumsaa\Uuid\Uuid */ public function generateNewId() { return Uuid::uuid4(); } protected function setModelParamsIdWithUuid($params) { if(!isset($params['id'])) { $this->setUuid($this->generateNewId()->toString()); $params['id'] = $this->getUuid(); } $this->uuid = $params['id']; return $params; } public function dealWithUsingUuidNotReturningIdFromCreate($model) { $this->saved = $model->find($this->uuid); } }

Here is an example of me using it.

/** * Incoming Post request from the Controller to a Service and finally to this Repository Class. */ public function createNewBatch($batch) { try { $this->post = $batch; //Incoming post data $this->setRelatedObjectsFromPost(); //I deal with some of the related data or incoming data before hand $setUuid = $this->setModelParamsIdWithUuid($batch); //this is the traits setting the UUID on the incoming POST data. $this->batch->create($setUuid); //Finally I create using the auto-generated UUID OR the id/uuid that came in via the post /** * Again the trait takes care of this ONE issue I can not get around is returning the results of Create with the an ID * It works and saves the data but the ID is missing. Though looking at Eloquent this most likely is because I need to be using save. * Any ways I load it back up again for further processing. * Also some of this work started when I was using Eloquent outside of Laravel so it may need to be refactored since we are back in Laravel. */ $this->dealWithUsingUuidNotReturningIdFromCreate($this->batch); $this->extractAndSyncRelatedObjects(); //This is the further processing I do not really related to this post return $this->uuid; //This was set in the Trait see the trait class for this uuid field. } catch (\Exception $e) { throw new \Exception('The batch could not be created ' . $e->getMessage()); } }

Also note the model class needs this property set

public $incrementing = false; END UPDATES

I needed to have UUID setup for users since we will be syncing users from site to site. I also wanted to use sentry as a starting point for user auth, roles and groups.

Using this starter package for a demo https://github.com/alnutile/L4withSentry/tree/uuid which is a fork of https://github.com/rydurham/L4withSentry

A lot of this comes from http://garrettstjohn.com/entry/using-uuids-laravel-eloquent-orm/

The repo will show the changes

Add the method and class to the base controller

This way we can call to it from other controllers as needed

Update the UserController

There are lots of changes in here to replace is_numeric wtih $this->isValue($id) to check the uuid

Update the seeder

This will setup our seed data to work as well

Update the user model

We call the boot method to generate a uuid if there is none being passed in.

Update October 10 10:18

This below setup for the model has not worked which is why I have the update at the start of this article

End update /** * The "booting" method of the model. * * @return void */ public static function boot() { parent::boot(); /** * Attach to the 'creating' Model Event to provide a UUID * for the `id` field (provided by $model->getKeyName()) */ static::creating(function ($model) { $model->id = (string)$model->generateNewId(); }); }

and then generateNewId method down below

/** * Get a new version 4 (random) UUID. * * @return \Rhumsaa\Uuid\Uuid */ public function generateNewId() { return Uuid::uuid4(); } Update Route file

Replace the regular expressions to allow uuid

+Route::get('users/{id}/reset/{code}', 'UserController@reset')->where('id', '[a-z0-9\-]+'); Route::get('users/{id}/suspend', array('as' => 'suspendUserForm', function($id) { return View::make('users.suspend')->with('id', $id); }));

There are a number of them in there

Composer

Of course you need it in your composer file

"require": { "laravel/framework": "4.2.*", "cartalyst/sentry": "~2.1", "r15ch13/peculiar": "1.0.*@dev" }, User Migration file

This repo the sentry migration files are copied into app/database/migrations

This being key

$table->string('id', '36')->primary();

#app/database/migrations/2012_12_06_225921_migration_cartalyst_sentry_install_users.php <?php use Illuminate\Database\Migrations\Migration; class MigrationCartalystSentryInstallUsers extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function($table) { $table->string('id', '36')->primary(); $table->string('email'); $table->string('password'); $table->text('permissions')->nullable(); $table->boolean('activated')->default(0); $table->string('activation_code')->nullable(); $table->timestamp('activated_at')->nullable(); $table->timestamp('last_login')->nullable(); $table->string('persist_code')->nullable(); $table->string('reset_password_code')->nullable(); $table->string('first_name')->nullable(); $table->string('last_name')->nullable(); $table->timestamps(); // We'll need to ensure that mysql uses the InnoDB engine to // support the indexes, other engines aren't affected. $table->engine = 'InnoDB'; $table->unique('email'); $table->index('activation_code'); $table->index('reset_password_code'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('users'); } }

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: LaravelInnoDBSQLMySQL
tags: gt,table,uuid,id,function,return,model,string,public,nullable
分页:12
转载请注明
本文标题:Laravel Sentry and UUID
本站链接:http://www.codesec.net/view/533741.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(130)