/** * Setup a starting Sentinel Auth environment: * Create the "Admin" role with a single permission "admin", * Create an "Admin" user having the provided email and password; * and attach it to the "Admin" role. * * When this is done, you'll have an administrator with "admin" permission. * Fron then on, you just need to make sure that administration tasks require the respective * permission: * From your controller: * * if ( !$this->auth->sentinel->check() ) { * return $this->auth->login(); * } elseif ( !$this->auth->sentinel->hasAccess(['admin']) ) { * return $response->withRedirect( $targetUrl ); * } * * The data tables must previously exists. The schema file is provided with Cartalyst Sentinel; * after you install the package, find the file vendor/cartalyst/sentinel/schema/mysql.sql */ public function setupEnvironment(ServerRequestInterface $request, ResponseInterface $response) { // // Create the Administration role $role = Auth::findRoleBySlug('admin'); if ($role) { echo "Role {$role['name']} already registered.\n"; } else { $role = Auth::getRoleRepository()->createModel()->create(['name' => 'Admin', 'slug' => 'admin']); if (!$role) { throw new \Exception("User creation failed."); } echo "Created role: Admin(admin).\n"; } // // Grant admins permissions to 'admin' $role->permissions = ['admin' => true]; $role->save(); echo "Granted permission 'admin' to role {$role['name']}.\n"; // // Create the user from given credentials $body = $request->getParsedBody(); $credentials = ['email' => $body['username'], 'password' => $body['password']]; $user = Auth::registerAndActivate($credentials); if (!$user) { throw new \Exception("User creation failed."); } echo "Created user: {$user['email']}.\n"; // // Make the user an administrator $role->users()->attach($user); echo "Attached user '{$user['email']}' to role '{$role['name']}'.\n"; echo "\n\n\nAuth environment ready. Please disable the route auth-env in AuthModule.php.\n\n"; }