snipe-it/database/factories/UserFactory.php

318 lines
8 KiB
PHP
Raw Normal View History

Discussion: Moving to policies for controller based authorization (#3080) * Make delete routes work. We put a little form in the modal that spoofs the delete field. * Fix route on creating a user. * Fix redundant id parameter. * Port acceptance tests to new urls. * Initial work on migrating to model based policies instead of global gates. Will allow for much more detailed permissions bits in the future. * This needs to stay for the dashboard checks. * Add user states for permissions to build tests. * Build up unit tests for gates/permissions. Move accessories/consumables/assets to policies instead of in authserviceprovider * Migrate various locations to new syntax. Update test to be more specific * Fix functional tests. Add an artisan command for installing a settings setup on travis-ci * Try a different id... Need to come up with a better way of passing the id for tests that need an existing one. * Try to fix travis * Update urls to use routes and not hardcode old paths. Also fix some migration errors found along the way.: * Add a environment for travis functional tests. * Adjust config file to make travis use it. * Use redirect()->route instead of redirect()-to * Dump all failures in the output directory if travis fails. * Cleanups and minor fixes. * Adjust the supplier modelfactory to comply with new validation restrictions. * Some test fixes. * Locales can be longer than 5 characters according to faker... fex gez_ET. Increase lenght in mysql and add a validation * Update test database dump to latest migrations.
2016-12-19 11:04:28 -08:00
<?php
2021-06-10 13:17:44 -07:00
namespace Database\Factories;
use App\Models\Company;
use App\Models\User;
2021-06-10 13:19:27 -07:00
use Illuminate\Database\Eloquent\Factories\Factory;
use \Auth;
2021-06-10 13:17:44 -07:00
class UserFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'activated' => 1,
'address' => $this->faker->address(),
'city' => $this->faker->city(),
'company_id' => Company::factory(),
'country' => $this->faker->country(),
2023-03-16 16:38:21 -07:00
'email' => $this->faker->safeEmail(),
2021-06-10 13:17:44 -07:00
'employee_num' => $this->faker->numberBetween(3500, 35050),
'first_name' => $this->faker->firstName(),
'jobtitle' => $this->faker->jobTitle(),
'last_name' => $this->faker->lastName(),
'locale' => 'en-US',
2021-06-10 13:17:44 -07:00
'notes' => 'Created by DB seeder',
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'permissions' => '{}',
2023-03-16 16:38:21 -07:00
'phone' => $this->faker->phoneNumber(),
'state' => $this->faker->stateAbbr(),
'username' => $this->faker->unique()->username(),
2023-03-16 16:38:21 -07:00
'zip' => $this->faker->postcode(),
2021-06-10 13:17:44 -07:00
];
}
public function deletedUser()
{
return $this->state(function () {
return [
'deleted_at' => $this->faker->dateTime(),
];
});
}
2021-06-10 13:17:44 -07:00
public function firstAdmin()
{
return $this->state(function () {
return [
'first_name' => 'Admin',
'last_name' => 'User',
'username' => 'admin',
'avatar' => '1.jpg',
2021-06-10 13:17:44 -07:00
'permissions' => '{"superuser":"1"}',
];
});
}
public function snipeAdmin()
{
return $this->state(function () {
return [
'first_name' => 'Snipe E.',
'last_name' => 'Head',
'username' => 'snipe',
'avatar' => '2.jpg',
2021-06-10 13:17:44 -07:00
'email' => 'snipe@snipe.net',
'permissions' => '{"superuser":"1"}',
];
});
}
public function testAdmin()
{
return $this->state(function () {
return [
'first_name' => 'Alison',
'last_name' => 'Gianotto',
'username' => 'agianotto@grokability.com',
'avatar' => '2.jpg',
'email' => 'agianotto@grokability.com',
'permissions' => '{"superuser":"1"}',
];
});
}
2021-06-10 13:17:44 -07:00
public function superuser()
{
return $this->appendPermission(['superuser' => '1']);
2021-06-10 13:17:44 -07:00
}
public function admin()
{
return $this->state(function () {
return [
'permissions' => '{"admin":"1"}',
'manager_id' => function () {
return User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin();
},
2021-06-10 13:17:44 -07:00
];
});
}
public function viewAssets()
{
return $this->appendPermission(['assets.view' => '1']);
2021-06-10 13:17:44 -07:00
}
public function createAssets()
{
return $this->appendPermission(['assets.create' => '1']);
2021-06-10 13:17:44 -07:00
}
public function editAssets()
{
return $this->appendPermission(['assets.edit' => '1']);
2021-06-10 13:17:44 -07:00
}
public function deleteAssets()
{
return $this->appendPermission(['assets.delete' => '1']);
2021-06-10 13:17:44 -07:00
}
public function checkinAssets()
{
return $this->appendPermission(['assets.checkin' => '1']);
2021-06-10 13:17:44 -07:00
}
public function checkoutAssets()
{
return $this->appendPermission(['assets.checkout' => '1']);
2021-06-10 13:17:44 -07:00
}
public function viewRequestableAssets()
{
return $this->appendPermission(['assets.view.requestable' => '1']);
2021-06-10 13:17:44 -07:00
}
public function viewAccessories()
{
return $this->appendPermission(['accessories.view' => '1']);
2021-06-10 13:17:44 -07:00
}
public function createAccessories()
{
return $this->appendPermission(['accessories.create' => '1']);
2021-06-10 13:17:44 -07:00
}
public function editAccessories()
{
return $this->appendPermission(['accessories.edit' => '1']);
2021-06-10 13:17:44 -07:00
}
public function deleteAccessories()
{
return $this->appendPermission(['accessories.delete' => '1']);
2021-06-10 13:17:44 -07:00
}
public function checkinAccessories()
{
return $this->appendPermission(['accessories.checkin' => '1']);
2021-06-10 13:17:44 -07:00
}
public function checkoutAccessories()
{
return $this->appendPermission(['accessories.checkout' => '1']);
2021-06-10 13:17:44 -07:00
}
public function viewConsumables()
{
return $this->appendPermission(['consumables.view' => '1']);
2021-06-10 13:17:44 -07:00
}
public function createConsumables()
{
return $this->appendPermission(['consumables.create' => '1']);
2021-06-10 13:17:44 -07:00
}
public function editConsumables()
{
return $this->appendPermission(['consumables.edit' => '1']);
2021-06-10 13:17:44 -07:00
}
public function deleteConsumables()
{
return $this->appendPermission(['consumables.delete' => '1']);
2021-06-10 13:17:44 -07:00
}
public function checkinConsumables()
{
return $this->appendPermission(['consumables.checkin' => '1']);
2021-06-10 13:17:44 -07:00
}
public function checkoutConsumables()
{
return $this->appendPermission(['consumables.checkout' => '1']);
2021-06-10 13:17:44 -07:00
}
public function viewDepartments()
{
return $this->appendPermission(['departments.view' => '1']);
}
2021-06-10 13:17:44 -07:00
public function viewLicenses()
{
return $this->appendPermission(['licenses.view' => '1']);
2021-06-10 13:17:44 -07:00
}
public function createLicenses()
{
return $this->appendPermission(['licenses.create' => '1']);
2021-06-10 13:17:44 -07:00
}
public function editLicenses()
{
return $this->appendPermission(['licenses.edit' => '1']);
2021-06-10 13:17:44 -07:00
}
public function deleteLicenses()
{
return $this->appendPermission(['licenses.delete' => '1']);
2021-06-10 13:17:44 -07:00
}
public function checkoutLicenses()
{
return $this->appendPermission(['licenses.checkout' => '1']);
2021-06-10 13:17:44 -07:00
}
public function viewKeysLicenses()
{
return $this->appendPermission(['licenses.keys' => '1']);
2021-06-10 13:17:44 -07:00
}
public function viewComponents()
{
return $this->appendPermission(['components.view' => '1']);
2021-06-10 13:17:44 -07:00
}
public function createComponents()
{
return $this->appendPermission(['components.create' => '1']);
2021-06-10 13:17:44 -07:00
}
public function editComponents()
{
return $this->appendPermission(['components.edit' => '1']);
2021-06-10 13:17:44 -07:00
}
public function deleteComponents()
{
return $this->appendPermission(['components.delete' => '1']);
2021-06-10 13:17:44 -07:00
}
public function checkinComponents()
{
return $this->appendPermission(['components.checkin' => '1']);
2021-06-10 13:17:44 -07:00
}
public function checkoutComponents()
{
return $this->appendPermission(['components.checkout' => '1']);
2021-06-10 13:17:44 -07:00
}
public function viewUsers()
{
return $this->appendPermission(['users.view' => '1']);
2021-06-10 13:17:44 -07:00
}
public function createUsers()
{
return $this->appendPermission(['users.create' => '1']);
2021-06-10 13:17:44 -07:00
}
public function editUsers()
{
return $this->appendPermission(['users.edit' => '1']);
2021-06-10 13:17:44 -07:00
}
public function deleteUsers()
{
return $this->appendPermission(['users.delete' => '1']);
2021-06-10 13:17:44 -07:00
}
public function canEditOwnLocation()
{
return $this->appendPermission(['self.edit_location' => '1']);
}
public function canViewReports()
{
return $this->appendPermission(['reports.view' => '1']);
}
private function appendPermission(array $permission)
{
return $this->state(function ($currentState) use ($permission) {
return [
'permissions' => json_encode(
array_merge(
json_decode($currentState['permissions'], true),
$permission
)
),
];
});
}
public function deleted(): self
{
return $this->state(['deleted_at' => $this->faker->dateTime()]);
}
2021-06-10 13:17:44 -07:00
}