From e994f2a88a3d009f249bccc03b8437e6cd62a2bf Mon Sep 17 00:00:00 2001 From: manavo Date: Tue, 25 Nov 2014 11:50:15 +0000 Subject: [PATCH] Save the user ID of who created the component or incident --- app/controllers/ApiController.php | 2 ++ ...11_25_114040_UserIdColumnForComponents.php | 36 +++++++++++++++++++ ..._11_25_114101_UserIdColumnForIncidents.php | 36 +++++++++++++++++++ app/models/Component.php | 3 +- app/models/Incident.php | 3 +- 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 app/database/migrations/2014_11_25_114040_UserIdColumnForComponents.php create mode 100644 app/database/migrations/2014_11_25_114101_UserIdColumnForIncidents.php diff --git a/app/controllers/ApiController.php b/app/controllers/ApiController.php index 3ad2cbf0..ce2778fa 100644 --- a/app/controllers/ApiController.php +++ b/app/controllers/ApiController.php @@ -43,6 +43,7 @@ */ public function postComponents() { $component = new Component(Input::all()); + $component->user_id = $this->auth->user()->id; if ($component->isValid()) { $component->saveOrFail(); return $component; @@ -82,6 +83,7 @@ */ public function postIncidents() { $incident = new Incident(Input::all()); + $incident->user_id = $this->auth->user()->id; if ($incident->isValid()) { $incident->saveOrFail(); return $incident; diff --git a/app/database/migrations/2014_11_25_114040_UserIdColumnForComponents.php b/app/database/migrations/2014_11_25_114040_UserIdColumnForComponents.php new file mode 100644 index 00000000..7f676f33 --- /dev/null +++ b/app/database/migrations/2014_11_25_114040_UserIdColumnForComponents.php @@ -0,0 +1,36 @@ +unsignedInteger('user_id')->nullable(); + + $table->foreign('user_id')->references('id')->on('users')->onDelete('SET NULL')->onUpdate('NO ACTION'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('components', function(Blueprint $table) + { + $table->dropColumn('user_id'); + }); + } + +} diff --git a/app/database/migrations/2014_11_25_114101_UserIdColumnForIncidents.php b/app/database/migrations/2014_11_25_114101_UserIdColumnForIncidents.php new file mode 100644 index 00000000..1fa032be --- /dev/null +++ b/app/database/migrations/2014_11_25_114101_UserIdColumnForIncidents.php @@ -0,0 +1,36 @@ +unsignedInteger('user_id')->nullable(); + + $table->foreign('user_id')->references('id')->on('users')->onDelete('SET NULL')->onUpdate('NO ACTION'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('incidents', function(Blueprint $table) + { + $table->dropColumn('user_id'); + }); + } + +} diff --git a/app/models/Component.php b/app/models/Component.php index 4f246b4a..e1aa495d 100644 --- a/app/models/Component.php +++ b/app/models/Component.php @@ -6,11 +6,12 @@ use ValidatingTrait; protected $rules = [ + 'user_id' => 'required|integer', 'name' => 'required', 'status' => 'required|integer' ]; - protected $fillable = ['name', 'description', 'status']; + protected $fillable = ['user_id', 'name', 'description', 'status']; /** * Lookup all of the incidents reported on the component. diff --git a/app/models/Incident.php b/app/models/Incident.php index 2807312f..0f9ab1f3 100644 --- a/app/models/Incident.php +++ b/app/models/Incident.php @@ -7,13 +7,14 @@ use \Illuminate\Database\Eloquent\SoftDeletingTrait; protected $rules = [ + 'user_id' => 'required|integer', 'component' => 'required|integer', 'name' => 'required', 'status' => 'required|integer', 'message' => 'required', ]; - protected $fillable = ['component', 'name', 'status', 'message']; + protected $fillable = ['user_id', 'component', 'name', 'status', 'message']; /** * An incident belongs to a component.