added ownership

This commit is contained in:
Denis Duliçi 2021-06-17 10:59:07 +03:00
parent de0d26b803
commit a3572de1ec
54 changed files with 253 additions and 37 deletions

View File

@ -33,6 +33,7 @@ abstract class Import implements HasLocalePreference, ShouldQueue, SkipsEmptyRow
public function map($row): array
{
$row['company_id'] = company_id();
$row['created_by'] = $this->user->id;
// Make enabled field integer
if (isset($row['enabled'])) {

View File

@ -3,16 +3,18 @@
namespace App\Abstracts;
use App\Traits\DateTime;
use App\Traits\Owners;
use App\Traits\Tenants;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kyslik\ColumnSortable\Sortable;
use Laratrust\Contracts\Ownable;
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
abstract class Model extends Eloquent
abstract class Model extends Eloquent implements Ownable
{
use Cachable, DateTime, SearchString, SoftDeletes, Sortable, Tenants;
use Cachable, DateTime, Owners, SearchString, SoftDeletes, Sortable, Tenants;
protected $tenantable = true;
@ -167,4 +169,23 @@ abstract class Model extends Eloquent
return $query->whereIn('contact_id', (array) $contacts);
}
public function scopeIsOwner($query)
{
return $query->where('created_by', user_id());
}
public function scopeIsNotOwner($query)
{
return $query->where('created_by', '<>', user_id());
}
public function ownerKey($owner)
{
if ($this->isNotOwnable()) {
return 0;
}
return $this->created_by;
}
}

View File

@ -19,6 +19,7 @@ class CreateAccount extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -7,9 +7,8 @@ use App\Jobs\Banking\CreateTransaction;
use App\Jobs\Document\CreateDocumentHistory;
use App\Events\Document\PaidAmountCalculated;
use App\Models\Banking\Transaction;
use App\Models\Document\Document;
use App\Traits\Currencies;
use Date;
use App\Utilities\Date;
class CreateBankingDocumentTransaction extends Job
{
@ -31,6 +30,7 @@ class CreateBankingDocumentTransaction extends Job
{
$this->model = $model;
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -20,6 +20,7 @@ class CreateReconciliation extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -21,6 +21,7 @@ class CreateTransaction extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -7,7 +7,6 @@ use App\Models\Banking\Account;
use App\Models\Banking\Transaction;
use App\Models\Banking\Transfer;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Traits\Currencies;
class CreateTransfer extends Job
@ -26,6 +25,7 @@ class CreateTransfer extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -22,6 +22,7 @@ class CreateCompany extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -22,6 +22,7 @@ class CreateContact extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**
@ -41,7 +42,7 @@ class CreateContact extends Job
// Upload logo
if ($this->request->file('logo')) {
$media = $this->getMedia($this->request->file('logo'), Str::plural($this->contact->type));
$this->contact->attachMedia($media, 'logo');
}
});

View File

@ -24,6 +24,7 @@ class CreateDashboard extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -20,6 +20,7 @@ class CreateItem extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -19,6 +19,7 @@ class CreateReport extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -23,6 +23,7 @@ class CreateDocument extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -19,6 +19,7 @@ class CreateCategory extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -19,6 +19,7 @@ class CreateCurrency extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -19,6 +19,7 @@ class CreateTax extends Job
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**

View File

@ -8,6 +8,7 @@ use App\Models\Common\Company;
use App\Models\Common\Report;
use App\Utilities\Reports as Utility;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;
class Version2117 extends Listener
{
@ -47,7 +48,7 @@ class Version2117 extends Listener
company($company_id)->makeCurrent();
}
protected function cacheReports()
{
Report::all()->each(function ($report) {

View File

@ -15,8 +15,6 @@ class Permission extends LaratrustPermission
protected $table = 'permissions';
protected $tenantable = false;
/**
* The accessors to append to the model's array form.
*

View File

@ -15,8 +15,6 @@ class Role extends LaratrustRole
protected $table = 'roles';
protected $tenantable = false;
/**
* The attributes that are mass assignable.
*

View File

@ -6,7 +6,7 @@ use App\Traits\Tenants;
use App\Notifications\Auth\Reset;
use App\Traits\Media;
use App\Traits\Users;
use Date;
use App\Utilities\Date;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
@ -22,8 +22,6 @@ class User extends Authenticatable implements HasLocalePreference
protected $table = 'users';
protected $tenantable = false;
/**
* The attributes that are mass assignable.
*

View File

@ -24,7 +24,7 @@ class Account extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'number', 'currency_code', 'opening_balance', 'bank_name', 'bank_phone', 'bank_address', 'enabled'];
protected $fillable = ['company_id', 'name', 'number', 'currency_code', 'opening_balance', 'bank_name', 'bank_phone', 'bank_address', 'enabled', 'created_by'];
/**
* The attributes that should be cast.

View File

@ -15,7 +15,7 @@ class Reconciliation extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'account_id', 'started_at', 'ended_at', 'closing_balance', 'reconciled'];
protected $fillable = ['company_id', 'account_id', 'started_at', 'ended_at', 'closing_balance', 'reconciled', 'created_by'];
/**
* The attributes that should be cast.

View File

@ -28,7 +28,23 @@ class Transaction extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'type', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'document_id', 'contact_id', 'description', 'category_id', 'payment_method', 'reference', 'parent_id'];
protected $fillable = [
'company_id',
'type',
'account_id',
'paid_at',
'amount',
'currency_code',
'currency_rate',
'document_id',
'contact_id',
'description',
'category_id',
'payment_method',
'reference',
'parent_id',
'created_by',
];
/**
* The attributes that should be cast.

View File

@ -18,7 +18,7 @@ class Transfer extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'expense_transaction_id', 'income_transaction_id'];
protected $fillable = ['company_id', 'expense_transaction_id', 'income_transaction_id', 'created_by'];
/**
* Sortable columns.

View File

@ -9,25 +9,25 @@ use App\Events\Common\CompanyMakingCurrent;
use App\Models\Document\Document;
use App\Traits\Contacts;
use App\Traits\Media;
use App\Traits\Owners;
use App\Traits\Tenants;
use App\Traits\Transactions;
use App\Utilities\Overrider;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kyslik\ColumnSortable\Sortable;
use Laratrust\Contracts\Ownable;
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
class Company extends Eloquent
class Company extends Eloquent implements Ownable
{
use Contacts, Media, SearchString, SoftDeletes, Sortable, Tenants, Transactions;
use Contacts, Media, Owners, SearchString, SoftDeletes, Sortable, Tenants, Transactions;
protected $table = 'companies';
protected $tenantable = false;
protected $dates = ['deleted_at'];
protected $fillable = ['domain', 'enabled'];
protected $fillable = ['domain', 'enabled', 'created_by'];
protected $casts = [
'enabled' => 'boolean',
@ -518,4 +518,23 @@ class Company extends Eloquent
{
return static::getCurrent() !== null;
}
public function scopeIsOwner($query)
{
return $query->where('created_by', user_id());
}
public function scopeIsNotOwner($query)
{
return $query->where('created_by', '<>', user_id());
}
public function ownerKey($owner)
{
if ($this->isNotOwnable()) {
return 0;
}
return $this->created_by;
}
}

View File

@ -24,7 +24,21 @@ class Contact extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'type', 'name', 'email', 'user_id', 'tax_number', 'phone', 'address', 'website', 'currency_code', 'reference', 'enabled'];
protected $fillable = [
'company_id',
'type',
'name',
'email',
'user_id',
'tax_number',
'phone',
'address',
'website',
'currency_code',
'reference',
'enabled',
'created_by',
];
/**
* The attributes that should be cast.

View File

@ -18,7 +18,7 @@ class Dashboard extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'enabled'];
protected $fillable = ['company_id', 'name', 'enabled', 'created_by'];
/**
* The attributes that should be cast.

View File

@ -27,7 +27,7 @@ class Item extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'description', 'sale_price', 'purchase_price', 'category_id', 'enabled'];
protected $fillable = ['company_id', 'name', 'description', 'sale_price', 'purchase_price', 'category_id', 'enabled', 'created_by'];
/**
* The attributes that should be cast.

View File

@ -18,7 +18,6 @@ class Recurring extends Model
*/
protected $fillable = ['company_id', 'recurable_id', 'recurable_type', 'frequency', 'interval', 'started_at', 'count'];
/**
* Get all of the owning recurable models.
*/

View File

@ -17,7 +17,7 @@ class Report extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'class', 'name', 'description', 'settings'];
protected $fillable = ['company_id', 'class', 'name', 'description', 'settings', 'created_by'];
/**
* The attributes that should be cast.

View File

@ -16,7 +16,7 @@ class Widget extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'dashboard_id', 'class', 'name', 'sort', 'settings'];
protected $fillable = ['company_id', 'dashboard_id', 'class', 'name', 'sort', 'settings', 'created_by'];
/**
* The attributes that should be cast.

View File

@ -41,6 +41,7 @@ class Document extends Model
'amount',
'currency_code',
'currency_rate',
'category_id',
'contact_id',
'contact_name',
'contact_email',
@ -48,9 +49,9 @@ class Document extends Model
'contact_phone',
'contact_address',
'notes',
'category_id',
'parent_id',
'footer',
'parent_id',
'created_by',
];
/**

View File

@ -18,7 +18,7 @@ class Category extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'type', 'color', 'enabled'];
protected $fillable = ['company_id', 'name', 'type', 'color', 'enabled', 'created_by'];
/**
* The attributes that should be cast.

View File

@ -19,7 +19,19 @@ class Currency extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'code', 'rate', 'enabled', 'precision', 'symbol', 'symbol_first', 'decimal_mark', 'thousands_separator'];
protected $fillable = [
'company_id',
'name',
'code',
'rate',
'enabled',
'precision',
'symbol',
'symbol_first',
'decimal_mark',
'thousands_separator',
'created_by',
];
/**
* The attributes that should be cast.

View File

@ -11,8 +11,6 @@ class Setting extends Eloquent
protected $table = 'settings';
protected $tenantable = true;
/**
* Attributes that should be mass-assignable.
*

View File

@ -24,7 +24,7 @@ class Tax extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'rate', 'type', 'enabled'];
protected $fillable = ['company_id', 'name', 'rate', 'type', 'enabled', 'created_by'];
/**
* The attributes that should be cast.

18
app/Traits/Owners.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace App\Traits;
trait Owners
{
public function isOwnable()
{
$ownable = $this->ownable ?: true;
return ($ownable === true) && in_array('created_by', $this->getFillable());
}
public function isNotOwnable()
{
return !$this->isOwnable();
}
}

View File

@ -18,7 +18,9 @@ trait Tenants
public function isTenantable()
{
return (isset($this->tenantable) && ($this->tenantable === true));
$tenantable = $this->tenantable ?: true;
return ($tenantable === true) && in_array('company_id', $this->getFillable());
}
public function isNotTenantable()

View File

@ -25,6 +25,7 @@ class Account extends TransformerAbstract
'bank_phone' => $model->bank_phone,
'bank_address' => $model->bank_address,
'enabled' => $model->enabled,
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -26,6 +26,7 @@ class Reconciliation extends TransformerAbstract
'ended_at' => $model->ended_at->toIso8601String(),
'closing_balance' => $model->closing_balance,
'reconciled' => $model->reconciled,
'created_by' => $model->created_by,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
];

View File

@ -37,6 +37,7 @@ class Transaction extends TransformerAbstract
'payment_method' => $model->payment_method,
'reference' => $model->reference,
'attachment' => $model->attachment,
'created_by' => $model->created_by,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
];

View File

@ -31,6 +31,7 @@ class Transfer extends TransformerAbstract
'amount' => $expense_transaction->amount,
'currency_code' => $expense_transaction->currency_code,
'paid_at' => $expense_transaction->paid_at ? $expense_transaction->paid_at->toIso8601String() : '',
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -22,6 +22,7 @@ class Company extends TransformerAbstract
'address' => $model->address,
'logo' => $model->logo,
'enabled' => $model->enabled,
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -27,6 +27,7 @@ class Contact extends TransformerAbstract
'currency_code' => $model->currency_code,
'enabled' => $model->enabled,
'reference' => $model->reference,
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -24,6 +24,7 @@ class Dashboard extends TransformerAbstract
'company_id' => $model->company_id,
'name' => $model->name,
'enabled' => $model->enabled,
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -30,6 +30,7 @@ class Item extends TransformerAbstract
'tax_ids' => $model->tax_ids,
'picture' => $model->picture,
'enabled' => $model->enabled,
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -22,6 +22,7 @@ class Report extends TransformerAbstract
'description' => $model->description,
'settings' => $model->settings,
'data' => $this->getReportData($model),
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -27,6 +27,7 @@ class Widget extends TransformerAbstract
'sort' => $model->sort,
'settings' => $model->settings,
'data' => show_widget($model),
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -41,6 +41,7 @@ class Document extends TransformerAbstract
'contact_address' => $model->contact_address,
'notes' => $model->notes,
'attachment' => $model->attachment,
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -20,6 +20,7 @@ class Category extends TransformerAbstract
'type' => $model->type,
'color' => $model->color,
'enabled' => $model->enabled,
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -25,6 +25,7 @@ class Currency extends TransformerAbstract
'symbol_first' => $model->symbol_first,
'decimal_mark' => $model->decimal_mark,
'thousands_separator' => $model->thousands_separator,
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -19,6 +19,7 @@ class Tax extends TransformerAbstract
'name' => $model->name,
'rate' => $model->rate,
'enabled' => $model->enabled,
'created_by' => $model->created_by,
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
];

View File

@ -24,6 +24,18 @@ if (!function_exists('user')) {
}
}
if (!function_exists('user_id')) {
/**
* Get id of current user.
*
* @return int
*/
function user_id()
{
return optional(user())->id;
}
}
if (!function_exists('company_date_format')) {
/**
* Format the given date based on company settings.

View File

@ -0,0 +1,82 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CoreV2117 extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('accounts', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('enabled');
});
Schema::table('categories', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('enabled');
});
Schema::table('companies', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('enabled');
});
Schema::table('contacts', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('reference');
});
Schema::table('currencies', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('enabled');
});
Schema::table('dashboards', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('enabled');
});
Schema::table('documents', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('parent_id');
});
Schema::table('items', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('enabled');
});
Schema::table('reconciliations', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('reconciled');
});
Schema::table('reports', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('settings');
});
Schema::table('taxes', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('enabled');
});
Schema::table('transactions', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('parent_id');
});
Schema::table('transfers', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('income_transaction_id');
});
Schema::table('widgets', function (Blueprint $table) {
$table->unsignedInteger('created_by')->nullable()->after('settings');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}