fixes for adding users into the production system

This commit is contained in:
David Bomba 2022-01-11 19:08:08 +11:00
parent 539fc1dcfd
commit f66c3076f1
4 changed files with 22 additions and 9 deletions

View File

@ -58,7 +58,7 @@ class StoreUserRequest extends Request
{
$input = $this->all();
//unique user rule - check company_user table for user_id / company_id / account_id if none exist we can add the user. ELSE return false
//unique user rule - check company_user table for user_id / company_id / account_id if none exist we can add the user. ELSE return false
if(array_key_exists('email', $input))
$input['email'] = trim($input['email']);

View File

@ -33,13 +33,18 @@ class CanAddUserRule implements Rule
public function passes($attribute, $value)
{
$count = CompanyUser::query()
->where('company_user.account_id', auth()->user()->account_id)
->join('users', 'users.id', '=', 'company_user.user_id')
->whereNull('users.deleted_at')
->whereNull('company_user.deleted_at')
->distinct()
->count('company_user.user_id');
/* If the user is active then we can add them to the company */
if(User::where('email', request()->input('email'))->where('account_id', auth()->user()->account_id)->where('is_deleted',0)->exists())
return true;
/* Check that we have sufficient quota to allow this to happen */
$count = CompanyUser::query()
->where('company_user.account_id', auth()->user()->account_id)
->join('users', 'users.id', '=', 'company_user.user_id')
->whereNull('users.deleted_at')
->whereNull('company_user.deleted_at')
->distinct()
->count('company_user.user_id');
return $count < auth()->user()->company()->account->num_users;

View File

@ -171,7 +171,7 @@ class Company extends BaseModel
public function users()
{
return $this->hasManyThrough(User::class, CompanyUser::class, 'company_id', 'id', 'id', 'user_id');
return $this->hasManyThrough(User::class, CompanyUser::class, 'company_id', 'id', 'id', 'user_id')->withTrashed();
}
public function expense_categories()

View File

@ -189,6 +189,14 @@ class UserRepository extends BaseRepository
return;
}
if (Ninja::isHosted()) {
$count = User::where('account_id', auth()->user()->account_id)->count();
if($count >= auth()->user()->account->num_users)
return;
}
$user->is_deleted = false;
$user->save();
$user->restore();