diff --git a/app/Models/Client.php b/app/Models/Client.php index 521527a2f125..d7d5cdef817a 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -147,6 +147,11 @@ class Client extends BaseModel return $this->belongsTo(User::class); } + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } + public function country() { return $this->belongsTo(Country::class); diff --git a/app/Models/Credit.php b/app/Models/Credit.php index 12fe1d35e3d1..dd0947ee815c 100644 --- a/app/Models/Credit.php +++ b/app/Models/Credit.php @@ -15,5 +15,8 @@ use Illuminate\Database\Eloquent\Model; class Credit extends BaseModel { - // + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } } diff --git a/app/Models/Expense.php b/app/Models/Expense.php index c3a0a84cfd31..aa07618e3338 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -38,4 +38,9 @@ class Expense extends BaseModel { return $this->morphMany(Document::class, 'documentable'); } + + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 7d42d058c2d8..79237e8561a4 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -135,6 +135,11 @@ class Invoice extends BaseModel return $this->belongsTo(User::class); } + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } + public function invitations() { return $this->hasMany(InvoiceInvitation::class); diff --git a/app/Models/Payment.php b/app/Models/Payment.php index d7e532931683..cf849b92d118 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -77,6 +77,11 @@ class Payment extends BaseModel return $this->belongsTo(User::class); } + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } + public function documents() { return $this->morphMany(Document::class, 'documentable'); diff --git a/app/Models/Product.php b/app/Models/Product.php index b630f48f0cc4..6359d9b5259a 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -50,6 +50,11 @@ class Product extends BaseModel return $this->belongsTo(User::class); } + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } + public function documents() { return $this->morphMany(Document::class, 'documentable'); diff --git a/app/Models/Proposal.php b/app/Models/Proposal.php index 1b8b7124e70e..c954cd11e8a0 100644 --- a/app/Models/Proposal.php +++ b/app/Models/Proposal.php @@ -39,4 +39,9 @@ class Proposal extends BaseModel return $this->morphMany(Document::class, 'documentable'); } + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } + } diff --git a/app/Models/Quote.php b/app/Models/Quote.php index 323ff1871899..cd0e0a5e4fa8 100644 --- a/app/Models/Quote.php +++ b/app/Models/Quote.php @@ -71,6 +71,11 @@ class Quote extends BaseModel return $this->belongsTo(User::class); } + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } + public function invitations() { return $this->hasMany(QuoteInvitation::class); diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index 082287946993..d4eaa2232517 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -110,6 +110,11 @@ class RecurringInvoice extends BaseModel return $this->belongsTo(User::class); } + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } + public function invoices() { return $this->hasMany(Invoice::class, "id", "recurring_invoice_id"); diff --git a/app/Models/RecurringQuote.php b/app/Models/RecurringQuote.php index 2288dfcc8ec5..f4ccdccbfe0c 100644 --- a/app/Models/RecurringQuote.php +++ b/app/Models/RecurringQuote.php @@ -107,6 +107,11 @@ class RecurringQuote extends BaseModel return $this->belongsTo(User::class); } + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } + public function invitations() { $this->morphMany(RecurringQuoteInvitation::class); diff --git a/app/Models/Task.php b/app/Models/Task.php index 6c296a4955fe..91c4cafaafb7 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -45,4 +45,9 @@ class Task extends BaseModel return $this->morphMany(Document::class, 'documentable'); } + public function assigned_user() + { + return $this->belongsTo(User::class ,'assigned_user_id', 'id'); + } + } diff --git a/app/Models/User.php b/app/Models/User.php index 84610b2b228a..7c59ceaf1754 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -28,7 +28,6 @@ use Illuminate\Notifications\Notifiable; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Log; use Laracasts\Presenter\PresentableTrait; class User extends Authenticatable implements MustVerifyEmail @@ -141,7 +140,6 @@ class User extends Authenticatable implements MustVerifyEmail */ public function setCompany($company) { - \Log::error('setting company'); $this->company = $company; } @@ -281,6 +279,19 @@ class User extends Authenticatable implements MustVerifyEmail } + /** + * Returns a boolean value if the user is assigned to the current Entity + * + * @param string Entity + * @return bool + */ + public function assigned($entity) : bool + { + + return ! empty($entity->assigned_user_id) && $entity->assigned_user_id == $this->id; + + } + /** * Flattens a stdClass representation of the User Permissions * into a Collection @@ -350,5 +361,6 @@ class User extends Authenticatable implements MustVerifyEmail return $this->email; } + } diff --git a/app/Policies/EntityPolicy.php b/app/Policies/EntityPolicy.php index c57d70905dd2..53a19007164d 100644 --- a/app/Policies/EntityPolicy.php +++ b/app/Policies/EntityPolicy.php @@ -50,7 +50,8 @@ class EntityPolicy return ($user->isAdmin() && $entity->company_id == $user->companyId()) || ($user->hasPermission('edit_' . strtolower(class_basename($entity))) && $entity->company_id == $user->companyId()) - || $user->owns($entity); + || $user->owns($entity) + || $user->assigned($entity); } @@ -68,7 +69,8 @@ class EntityPolicy return ($user->isAdmin() && $entity->company_id == $user->companyId()) || ($user->hasPermission('view_' . strtolower(class_basename($entity))) && $entity->company_id == $user->companyId()) - || $user->owns($entity); + || $user->owns($entity) + || $user->assigned($entity); } diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index b2bfcb82cf51..54ef71c7b76a 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -280,6 +280,7 @@ class CreateUsersTable extends Migration $table->increments('id'); $table->unsignedInteger('company_id')->index(); $table->unsignedInteger('user_id')->index(); + $table->unsignedInteger('assigned_user_id')->nullable(); $table->string('name')->nullable(); $table->string('website')->nullable(); @@ -412,6 +413,7 @@ class CreateUsersTable extends Migration $t->increments('id'); $t->unsignedInteger('client_id')->index(); $t->unsignedInteger('user_id'); + $t->unsignedInteger('assigned_user_id')->nullable(); $t->unsignedInteger('company_id')->index(); $t->unsignedInteger('status_id'); @@ -478,6 +480,7 @@ class CreateUsersTable extends Migration $t->increments('id'); $t->unsignedInteger('client_id')->index(); $t->unsignedInteger('user_id'); + $t->unsignedInteger('assigned_user_id')->nullable(); $t->unsignedInteger('company_id')->index(); $t->unsignedInteger('status_id')->index(); @@ -541,6 +544,7 @@ class CreateUsersTable extends Migration $t->increments('id'); $t->unsignedInteger('client_id')->index(); $t->unsignedInteger('user_id'); + $t->unsignedInteger('assigned_user_id')->nullable(); $t->unsignedInteger('company_id')->index(); $t->unsignedInteger('status_id')->index(); @@ -602,6 +606,7 @@ class CreateUsersTable extends Migration $t->increments('id'); $t->unsignedInteger('client_id')->index(); $t->unsignedInteger('user_id'); + $t->unsignedInteger('assigned_user_id')->nullable(); $t->unsignedInteger('company_id')->index(); $t->unsignedInteger('status_id'); $t->unsignedInteger('design_id'); @@ -708,6 +713,7 @@ class CreateUsersTable extends Migration $t->increments('id'); $t->unsignedInteger('company_id')->index(); $t->unsignedInteger('user_id'); + $t->unsignedInteger('assigned_user_id')->nullable(); $t->string('custom_value1')->nullable(); $t->string('custom_value2')->nullable(); @@ -743,6 +749,7 @@ class CreateUsersTable extends Migration $t->unsignedInteger('company_id')->index(); $t->unsignedInteger('client_id')->index(); $t->unsignedInteger('user_id')->nullable(); + $t->unsignedInteger('assigned_user_id')->nullable(); $t->unsignedInteger('client_contact_id')->nullable(); $t->unsignedInteger('invitation_id')->nullable(); $t->unsignedInteger('company_gateway_id')->nullable(); @@ -784,6 +791,7 @@ class CreateUsersTable extends Migration Schema::create('tasks', function ($table) { $table->increments('id'); $table->unsignedInteger('user_id'); + $table->unsignedInteger('assigned_user_id')->nullable(); $table->unsignedInteger('company_id')->index(); $table->unsignedInteger('client_id')->nullable(); $table->unsignedInteger('invoice_id')->nullable(); diff --git a/routes/api.php b/routes/api.php index 0d5201714657..1c305e49567e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -84,7 +84,7 @@ Route::group(['middleware' => ['api_db','api_secret_check','token_auth'], 'prefi Route::post('refresh', 'Auth\LoginController@refresh'); Route::get('templates/{entity}/create', 'TemplateController@create')->name('templates.create'); - Route::get('templates/{entity}/{entity_id}', 'TemplateController@show')->name('templates.show'); + Route::post('templates/{entity}/{entity_id}', 'TemplateController@show')->name('templates.show'); /* Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit