Beispiel #1
0
 public function run()
 {
     License::truncate();
     factory(License::class, 'license', 10)->create();
     LicenseSeat::truncate();
     factory(LicenseSeat::class, 'license-seat', 10)->create();
     // factory(App\Models\Asset::class, 50)->create()->each(function($u) {
     //   $u->assetmodel()->save(factory(App\AssetModel::class)->make());
     // });
 }
Beispiel #2
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     if ($this->confirm("\n****************************************************\nTHIS WILL DELETE ALL OF THE DATA IN YOUR DATABASE. \nThere is NO undo. This WILL destroy ALL of your data. \n****************************************************\n\nDo you wish to continue? No backsies! [y|N]")) {
         if ($this->option('soft')) {
             Accessory::getQuery()->delete();
             Asset::getQuery()->delete();
             Category::getQuery()->delete();
             Company::getQuery()->delete();
             Component::getQuery()->delete();
             Consumable::getQuery()->delete();
             Depreciation::getQuery()->delete();
             License::getQuery()->delete();
             LicenseSeat::getQuery()->delete();
             Location::getQuery()->delete();
             Manufacturer::getQuery()->delete();
             AssetModel::getQuery()->delete();
             Statuslabel::getQuery()->delete();
             Supplier::getQuery()->delete();
             Group::getQuery()->delete();
             DB::statement('delete from accessories_users');
             DB::statement('delete from asset_logs');
             DB::statement('delete from asset_maintenances');
             DB::statement('delete from asset_uploads');
             DB::statement('delete from consumables_users');
             DB::statement('delete from custom_field_custom_fieldset');
             DB::statement('delete from custom_fields');
             DB::statement('delete from custom_fieldsets');
             DB::statement('delete from components_assets');
             DB::statement('delete from password_resets');
             DB::statement('delete from requested_assets');
             DB::statement('delete from requests');
             DB::statement('delete from throttle');
             DB::statement('delete from users_groups');
             DB::statement('delete from users WHERE id!=1');
         } else {
             \DB::statement('drop table IF EXISTS accessories_users');
             \DB::statement('drop table IF EXISTS accessories');
             \DB::statement('drop table IF EXISTS asset_logs');
             \DB::statement('drop table IF EXISTS asset_maintenances');
             \DB::statement('drop table IF EXISTS asset_uploads');
             \DB::statement('drop table IF EXISTS assets');
             \DB::statement('drop table IF EXISTS categories');
             \DB::statement('drop table IF EXISTS companies');
             \DB::statement('drop table IF EXISTS consumables_users');
             \DB::statement('drop table IF EXISTS consumables');
             \DB::statement('drop table IF EXISTS custom_field_custom_fieldset');
             \DB::statement('drop table IF EXISTS custom_fields');
             \DB::statement('drop table IF EXISTS custom_fieldsets');
             \DB::statement('drop table IF EXISTS depreciations');
             \DB::statement('drop table IF EXISTS groups');
             \DB::statement('drop table IF EXISTS history');
             \DB::statement('drop table IF EXISTS components');
             \DB::statement('drop table IF EXISTS components_assets');
             \DB::statement('drop table IF EXISTS license_seats');
             \DB::statement('drop table IF EXISTS licenses');
             \DB::statement('drop table IF EXISTS locations');
             \DB::statement('drop table IF EXISTS manufacturers');
             \DB::statement('drop table IF EXISTS models');
             \DB::statement('drop table IF EXISTS migrations');
             \DB::statement('drop table IF EXISTS password_resets');
             \DB::statement('drop table IF EXISTS requested_assets');
             \DB::statement('drop table IF EXISTS requests');
             \DB::statement('drop table IF EXISTS settings');
             \DB::statement('drop table IF EXISTS status_labels');
             \DB::statement('drop table IF EXISTS suppliers');
             \DB::statement('drop table IF EXISTS throttle');
             \DB::statement('drop table IF EXISTS users_groups');
             \DB::statement('drop table IF EXISTS users');
         }
     }
 }
Beispiel #3
0
 /**
  * Get the number of assigned seats
  *
  */
 public function assignedcount()
 {
     return \App\Models\LicenseSeat::where('license_id', '=', $this->id)->where(function ($query) {
         $query->whereNotNull('assigned_to')->orWhereNotNull('asset_id');
     })->count();
 }
Beispiel #4
0
 public function freeSeat()
 {
     $seat = LicenseSeat::where('license_id', '=', $this->id)->whereNull('deleted_at')->whereNull('assigned_to')->whereNull('asset_id')->first();
     return $seat->id;
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $filename = $this->argument('filename');
     if (!$this->option('testrun') == 'true') {
         $this->comment('======= Importing Licenses from ' . $filename . ' =========');
     } else {
         $this->comment('====== TEST ONLY License Import for ' . $filename . ' ====');
         $this->comment('============== NO DATA WILL BE WRITTEN ==============');
     }
     if (!ini_get("auto_detect_line_endings")) {
         ini_set("auto_detect_line_endings", '1');
     }
     $csv = Reader::createFromPath($this->argument('filename'));
     $csv->setNewline("\r\n");
     $csv->setOffset(1);
     $duplicates = '';
     // Loop through the records
     $nbInsert = $csv->each(function ($row) use($duplicates) {
         $status_id = 1;
         // Let's just map some of these entries to more user friendly words
         if (array_key_exists('0', $row)) {
             $user_name = trim($row[0]);
         } else {
             $user_name = '';
         }
         if (array_key_exists('1', $row)) {
             $user_email = trim($row[1]);
         } else {
             $user_email = '';
         }
         if (array_key_exists('2', $row)) {
             $user_username = trim($row[2]);
         } else {
             $user_username = '';
         }
         if (array_key_exists('3', $row)) {
             $user_license_name = trim($row[3]);
         } else {
             $user_license_name = '';
         }
         if (array_key_exists('4', $row)) {
             $user_license_serial = trim($row[4]);
         } else {
             $user_license_serial = '';
         }
         if (array_key_exists('5', $row)) {
             $user_licensed_to_name = trim($row[5]);
         } else {
             $user_licensed_to_name = '';
         }
         if (array_key_exists('6', $row)) {
             $user_licensed_to_email = trim($row[6]);
         } else {
             $user_licensed_to_email = '';
         }
         if (array_key_exists('7', $row)) {
             $user_license_seats = trim($row[7]);
         } else {
             $user_license_seats = '';
         }
         if (array_key_exists('8', $row)) {
             $user_license_reassignable = trim($row[8]);
             if ($user_license_reassignable != '') {
                 if (strtolower($user_license_reassignable) == 'yes' || strtolower($user_license_reassignable) == 'true' || $user_license_reassignable == '1') {
                     $user_license_reassignable = 1;
                 }
             } else {
                 $user_license_reassignable = 0;
             }
         } else {
             $user_license_reassignable = 0;
         }
         if (array_key_exists('9', $row)) {
             $user_license_supplier = trim($row[9]);
         } else {
             $user_license_supplier = '';
         }
         if (array_key_exists('10', $row)) {
             $user_license_maintained = trim($row[10]);
             if ($user_license_maintained != '') {
                 if (strtolower($user_license_maintained) == 'yes' || strtolower($user_license_maintained) == 'true' || $user_license_maintained == '1') {
                     $user_license_maintained = 1;
                 }
             } else {
                 $user_license_maintained = 0;
             }
         } else {
             $user_license_maintained = '';
         }
         if (array_key_exists('11', $row)) {
             $user_license_notes = trim($row[11]);
         } else {
             $user_license_notes = '';
         }
         if (array_key_exists('12', $row)) {
             if ($row[12] != '') {
                 $user_license_purchase_date = date("Y-m-d 00:00:01", strtotime($row[12]));
             } else {
                 $user_license_purchase_date = '';
             }
         } else {
             $user_license_purchase_date = 0;
         }
         // A number was given instead of a name
         if (is_numeric($user_name)) {
             $this->comment('User ' . $user_name . ' is not a name - assume this user already exists');
             $user_username = '';
             // No name was given
         } elseif ($user_name == '') {
             $this->comment('No user data provided - skipping user creation, just adding license');
             $first_name = '';
             $last_name = '';
             $user_username = '';
         } else {
             $name = explode(" ", $user_name);
             $first_name = $name[0];
             $email_last_name = '';
             $email_prefix = $first_name;
             if (!array_key_exists(1, $name)) {
                 $last_name = '';
                 $email_last_name = $last_name;
                 $email_prefix = $first_name;
             } else {
                 $last_name = str_replace($first_name, '', $user_name);
                 if ($this->option('email_format') == 'filastname') {
                     $email_last_name .= str_replace(' ', '', $last_name);
                     $email_prefix = $first_name[0] . $email_last_name;
                 } elseif ($this->option('email_format') == 'firstname.lastname') {
                     $email_last_name .= str_replace(' ', '', $last_name);
                     $email_prefix = $first_name . '.' . $email_last_name;
                 } elseif ($this->option('email_format') == 'firstname') {
                     $email_last_name .= str_replace(' ', '', $last_name);
                     $email_prefix = $first_name;
                 }
             }
             $user_username = $email_prefix;
             // Generate an email based on their name if no email address is given
             if ($user_email == '') {
                 if ($first_name == 'Unknown') {
                     $status_id = 7;
                 }
                 $email = strtolower($email_prefix) . '@' . $this->option('domain');
                 $user_email = str_replace("'", '', $email);
             }
         }
         $this->comment('Full Name: ' . $user_name);
         $this->comment('First Name: ' . $first_name);
         $this->comment('Last Name: ' . $last_name);
         $this->comment('Username: '******'Email: ' . $user_email);
         $this->comment('License Name: ' . $user_license_name);
         $this->comment('Serial No: ' . $user_license_serial);
         $this->comment('Licensed To Name: ' . $user_licensed_to_name);
         $this->comment('Licensed To Email: ' . $user_licensed_to_email);
         $this->comment('Seats: ' . $user_license_seats);
         $this->comment('Reassignable: ' . $user_license_reassignable);
         $this->comment('Supplier: ' . $user_license_supplier);
         $this->comment('Maintained: ' . $user_license_maintained);
         $this->comment('Notes: ' . $user_license_notes);
         $this->comment('Purchase Date: ' . $user_license_purchase_date);
         $this->comment('------------- Action Summary ----------------');
         if ($user_username != '') {
             if ($user = User::where('username', $user_username)->whereNotNull('username')->first()) {
                 $this->comment('User ' . $user_username . ' already exists');
             } else {
                 $user = new \App\Models\User();
                 $password = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20);
                 $user->first_name = $first_name;
                 $user->last_name = $last_name;
                 $user->username = $user_username;
                 $user->email = $user_email;
                 $user->permissions = '{user":1}';
                 $user->password = bcrypt($password);
                 $user->activated = 1;
                 if ($user->save()) {
                     $this->comment('User ' . $first_name . ' created');
                 } else {
                     $this->error('ERROR CREATING User ' . $first_name . ' ' . $last_name);
                     $this->error($user->getErrors());
                 }
                 $this->comment('User ' . $first_name . ' created');
             }
         } else {
             $user = new User();
             $user->user_id = null;
         }
         // Check for the supplier match and create it if it doesn't exist
         if ($supplier = Supplier::where('name', $user_license_supplier)->first()) {
             $this->comment('Supplier ' . $user_license_supplier . ' already exists');
         } else {
             $supplier = new Supplier();
             $supplier->name = e($user_license_supplier);
             $supplier->user_id = 1;
             if ($supplier->save()) {
                 $this->comment('Supplier ' . $user_license_supplier . ' was created');
             } else {
                 $this->comment('Something went wrong! Supplier ' . $user_license_supplier . ' was NOT created');
             }
         }
         // Add the license
         $license = new License();
         $license->name = e($user_license_name);
         if ($user_license_purchase_date != '') {
             $license->purchase_date = $user_license_purchase_date;
         } else {
             $license->purchase_date = null;
         }
         $license->serial = e($user_license_serial);
         $license->seats = e($user_license_seats);
         $license->supplier_id = $supplier->id;
         $license->user_id = 1;
         if ($user_license_purchase_date != '') {
             $license->purchase_date = $user_license_purchase_date;
         } else {
             $license->purchase_date = null;
         }
         $license->license_name = $user_licensed_to_name;
         $license->license_email = $user_licensed_to_email;
         $license->notes = e($user_license_notes);
         if ($license->save()) {
             $this->comment('License ' . $user_license_name . ' with serial number ' . $user_license_serial . ' was created');
             $license_seat_created = 0;
             for ($x = 0; $x < $user_license_seats; $x++) {
                 // Create the license seat entries
                 $license_seat = new LicenseSeat();
                 $license_seat->license_id = $license->id;
                 // Only assign the first seat to the user
                 if ($x == 0) {
                     $license_seat->assigned_to = $user->id;
                 } else {
                     $license_seat->assigned_to = null;
                 }
                 if ($license_seat->save()) {
                     $license_seat_created++;
                 }
             }
             if ($license_seat_created > 0) {
                 $this->comment($license_seat_created . ' seats were created');
             } else {
                 $this->comment('Something went wrong! NO seats for ' . $user_license_name . ' were created');
             }
         } else {
             $this->comment('Something went wrong! License ' . $user_license_name . ' was NOT created');
         }
         $this->comment('=====================================');
         return true;
     });
 }
 /**
  * Validates and stores the license checkin action.
  *
  * @author [A. Gianotto] [<*****@*****.**>]
  * @see LicensesController::getCheckin() method that provides the form view
  * @since [v1.0]
  * @param int $seatId
  * @param string $backto
  * @return Redirect
  */
 public function postCheckin($seatId = null, $backto = null)
 {
     // Check if the asset exists
     if (is_null($licenseseat = LicenseSeat::find($seatId))) {
         // Redirect to the asset management page with error
         return redirect()->to('admin/licenses')->with('error', trans('admin/licenses/message.not_found'));
     }
     $license = License::find($licenseseat->license_id);
     if (!Company::isCurrentUserHasAccess($license)) {
         return redirect()->to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
     }
     if (!$license->reassignable) {
         // Not allowed to checkin
         Session::flash('error', 'License not reassignable.');
         return redirect()->back()->withInput();
     }
     // Declare the rules for the form validation
     $rules = array('note' => 'string', 'notes' => 'string');
     // Create a new validator instance from our validation rules
     $validator = Validator::make(Input::all(), $rules);
     // If validation fails, we'll exit the operation now.
     if ($validator->fails()) {
         // Ooops.. something went wrong
         return redirect()->back()->withInput()->withErrors($validator);
     }
     $return_to = $licenseseat->assigned_to;
     $logaction = new Actionlog();
     $logaction->checkedout_to = $licenseseat->assigned_to;
     // Update the asset data
     $licenseseat->assigned_to = null;
     $licenseseat->asset_id = null;
     $user = Auth::user();
     // Was the asset updated?
     if ($licenseseat->save()) {
         $logaction->asset_id = $licenseseat->license_id;
         $logaction->location_id = null;
         $logaction->asset_type = 'software';
         $logaction->note = e(Input::get('note'));
         $logaction->user_id = $user->id;
         $settings = Setting::getSettings();
         if ($settings->slack_endpoint) {
             $slack_settings = ['username' => $settings->botname, 'channel' => $settings->slack_channel, 'link_names' => true];
             $client = new \Maknz\Slack\Client($settings->slack_endpoint, $slack_settings);
             try {
                 $client->attach(['color' => 'good', 'fields' => [['title' => 'Checked In:', 'value' => strtoupper($logaction->asset_type) . ' <' . config('app.url') . '/admin/licenses/' . $license->id . '/view' . '|' . $license->name . '> checked in by <' . config('app.url') . '/admin/users/' . $user->id . '/view' . '|' . $user->fullName() . '>.'], ['title' => 'Note:', 'value' => e($logaction->note)]]])->send('License Checked In');
             } catch (Exception $e) {
             }
         }
         $log = $logaction->logaction('checkin from');
         if ($backto == 'user') {
             return redirect()->to("admin/users/" . $return_to . '/view')->with('success', trans('admin/licenses/message.checkin.success'));
         } else {
             return redirect()->to("admin/licenses/" . $licenseseat->license_id . "/view")->with('success', trans('admin/licenses/message.checkin.success'));
         }
     }
     // Redirect to the license page with error
     return redirect()->to("admin/licenses")->with('error', trans('admin/licenses/message.checkin.error'));
 }
Beispiel #7
0
 /**
  * Soft-delete bulk users
  *
  * @author [A. Gianotto] [<*****@*****.**>]
  * @since [v1.0]
  * @return Redirect
  */
 public function postBulkSave()
 {
     if (!Input::has('edit_user') || count(Input::has('edit_user')) == 0) {
         return redirect()->back()->with('error', 'No users selected');
     } elseif (!Input::has('status_id') || count(Input::has('status_id')) == 0) {
         return redirect()->route('users')->with('error', 'No status selected');
     } else {
         $user_raw_array = Input::get('edit_user');
         $asset_array = array();
         if (($key = array_search(Auth::user()->id, $user_raw_array)) !== false) {
             unset($user_raw_array[$key]);
         }
         if (!Auth::user()->isSuperUser()) {
             return redirect()->route('users')->with('error', trans('admin/users/message.insufficient_permissions'));
         }
         if (!config('app.lock_passwords')) {
             $users = User::whereIn('id', $user_raw_array)->get();
             $assets = Asset::whereIn('assigned_to', $user_raw_array)->get();
             $accessories = DB::table('accessories_users')->whereIn('assigned_to', $user_raw_array)->get();
             $licenses = DB::table('license_seats')->whereIn('assigned_to', $user_raw_array)->get();
             $license_array = array();
             $accessory_array = array();
             foreach ($assets as $asset) {
                 $asset_array[] = $asset->id;
                 // Update the asset log
                 $logaction = new Actionlog();
                 $logaction->asset_id = $asset->id;
                 $logaction->checkedout_to = $asset->assigned_to;
                 $logaction->asset_type = 'hardware';
                 $logaction->user_id = Auth::user()->id;
                 $logaction->note = 'Bulk checkin asset and delete user';
                 $logaction->logaction('checkin from');
                 Asset::whereIn('id', $asset_array)->update(array('status_id' => e(Input::get('status_id')), 'assigned_to' => null));
             }
             foreach ($accessories as $accessory) {
                 $accessory_array[] = $accessory->accessory_id;
                 // Update the asset log
                 $logaction = new Actionlog();
                 $logaction->accessory_id = $accessory->id;
                 $logaction->checkedout_to = $accessory->assigned_to;
                 $logaction->asset_type = 'accessory';
                 $logaction->user_id = Auth::user()->id;
                 $logaction->note = 'Bulk checkin accessory and delete user';
                 $logaction->logaction('checkin from');
             }
             foreach ($licenses as $license) {
                 $license_array[] = $license->id;
                 // Update the asset log
                 $logaction = new Actionlog();
                 $logaction->asset_id = $license->id;
                 $logaction->checkedout_to = $license->assigned_to;
                 $logaction->asset_type = 'software';
                 $logaction->user_id = Auth::user()->id;
                 $logaction->note = 'Bulk checkin license and delete user';
                 $logaction->logaction('checkin from');
             }
             LicenseSeat::whereIn('id', $license_array)->update(['assigned_to' => null]);
             foreach ($users as $user) {
                 $user->accessories()->sync(array());
                 $user->delete();
             }
             return redirect()->route('users')->with('success', 'Your selected users have been deleted and their assets have been updated.');
         } else {
             return redirect()->route('users')->with('error', 'Bulk delete is not enabled in this installation');
         }
     }
 }