Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Ldap Company as option and FMCS Notification #14650

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/Console/Commands/LdapSync.php
Expand Up @@ -2,6 +2,7 @@

namespace App\Console\Commands;

use App\Models\Company;
use App\Models\Department;
use App\Models\Group;
use Illuminate\Console\Command;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function handle()
$ldap_result_jobtitle = Setting::getSettings()->ldap_jobtitle;
$ldap_result_country = Setting::getSettings()->ldap_country;
$ldap_result_location = Setting::getSettings()->ldap_location;
$ldap_result_company = Setting::getSettings()->ldap_company;
$ldap_result_dept = Setting::getSettings()->ldap_dept;
$ldap_result_manager = Setting::getSettings()->ldap_manager;
$ldap_default_group = Setting::getSettings()->ldap_default_group;
Expand Down Expand Up @@ -232,13 +234,19 @@ public function handle()
$item['department'] = $results[$i][$ldap_result_dept][0] ?? '';
$item['manager'] = $results[$i][$ldap_result_manager][0] ?? '';
$item['location'] = $results[$i][$ldap_result_location][0] ?? '';
$item['company'] = $results[$i][$ldap_result_company][0] ?? '';

// ONLY if you are using the "ldap_location" option *AND* you have an actual result
if ($ldap_result_location && $item['location']) {
$location = Location::firstOrCreate([
'name' => $item['location'],
]);
}
if ($ldap_result_company && $item['company']) {
$company = Company::firstOrCreate([
'name' => $item['company'],
]);
}
$department = Department::firstOrCreate([
'name' => $item['department'],
]);
Expand Down Expand Up @@ -286,6 +294,9 @@ public function handle()
if($ldap_result_location != null){
$user->location_id = $location ? $location->id : null;
}
if($ldap_result_company != null){
$user->company_id = $company ? $company->id : null;
}

if($ldap_result_manager != null){
if($item['manager'] != null) {
Expand Down
101 changes: 101 additions & 0 deletions app/Helpers/Helper.php
Expand Up @@ -2,18 +2,23 @@

namespace App\Helpers;
use App\Models\Accessory;
use App\Models\AccessoryCheckout;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Component;
use App\Models\Consumable;
use App\Models\CustomField;
use App\Models\CustomFieldset;
use App\Models\Depreciation;
use App\Models\LicenseSeat;
use App\Models\Location;
use App\Models\Setting;
use App\Models\Statuslabel;
use App\Models\User;
use App\Models\License;
use Crypt;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Database\Eloquent\Builder;
use Image;
use Carbon\Carbon;

Expand Down Expand Up @@ -827,6 +832,102 @@ public static function checkLowInventory()

return $items_array;
}
/**
* Looks for any items that have a different company_id than the users they are assigned to for the
* alert dropdown
*
* @author [G. Martinez]
* @since [v6.4]
* @return array
*/
public static function checkUserCompanyAssets(): array
{ if(Setting::getSettings()->full_multiple_companies_support == 1) {

$accessories = AccessoryCheckout::with('accessory', 'user')
->join('users', 'accessories_users.assigned_to', '=', 'users.id')
->whereHas('accessory', function ($query) {
$query->where(function ($query) {
$query->WhereColumn('accessories.company_id', '!=', 'users.company_id');
});
})
->get();

$assets = Asset::with('assignedTo', 'company', 'model')
->whereHasMorph(
'assignedTo',
[Asset::class, User::class],
function (Builder $query, $type) {
$tableName = (new $type)->getTable();

if ($type === Asset::class) {
// If checked out to Asset, we are making an alias for the assigned_asset.
// we use the assigned_to to join on the assigned_asset ID
$query->join($tableName . ' as ' . 'assigned_asset', function ($join) {
$join->on('assets.assigned_to', '=', 'assigned_asset.id');
})
// continue with company_id check for an Asset.
->whereColumn('assets.company_id', '!=', 'assigned_asset.company_id')
->whereColumn('assets.id', '!=', 'assigned_asset.id');
} else {
// For users, we do not need an alias
$query->whereColumn('assets.company_id', '!=', $tableName . '.company_id');
}
}
)->get();

$licenses = LicenseSeat::with('license', 'user')
->join('users', 'license_seats.assigned_to', '=', 'users.id')
->whereHas('license', function ($query) {
$query->where(function ($query) {
$query->WhereColumn('licenses.company_id', '!=', 'users.company_id');
});
})
->get();

$items_array = [];
$all_count = 0;

foreach ($accessories as $accessory) {

$items_array[$all_count]['id'] = $accessory->accessory->id;
$items_array[$all_count]['name'] = $accessory->accessory->name;
$items_array[$all_count]['company'] = $accessory->accessory->company->name;
$items_array[$all_count]['user'] = $accessory->user->present()->fullName;
$items_array[$all_count]['user_company'] = $accessory->user->company->name;
$items_array[$all_count]['type'] = 'accessories';

$all_count++;

}
foreach ($assets as $asset) {

$items_array[$all_count]['id'] = $asset->id;
$items_array[$all_count]['name'] = $asset->model->name;
$items_array[$all_count]['company'] = $asset->company->name;
$items_array[$all_count]['user'] = $asset->assignedto->present()->fullName;
$items_array[$all_count]['user_company'] = $asset->assignedto->company->name;
$items_array[$all_count]['type'] = 'hardware';
// dd($asset->company->id, $asset->assignedto->company->id);
$all_count++;

}

foreach ($licenses as $license) {
$items_array[$all_count]['id'] = $license->license->id;
$items_array[$all_count]['name'] = $license->license->name;
$items_array[$all_count]['company'] = $license->license->company->name;
$items_array[$all_count]['user'] = $license->user->present()->fullName;
$items_array[$all_count]['user_company'] = $license->user->company->name;
$items_array[$all_count]['type'] = 'licenses';

$all_count++;

}

return $items_array;
}
return array();
}

/**
* Check if the file is an image, so we can show a preview
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/SettingsController.php
Expand Up @@ -966,6 +966,7 @@ public function postLdapSettings(Request $request)
$setting->ldap_jobtitle = $request->input('ldap_jobtitle');
$setting->ldap_country = $request->input('ldap_country');
$setting->ldap_location = $request->input('ldap_location');
$setting->ldap_company = $request->input('ldap_company');
$setting->ldap_dept = $request->input('ldap_dept');
$setting->ldap_client_tls_cert = $request->input('ldap_client_tls_cert');
$setting->ldap_client_tls_key = $request->input('ldap_client_tls_key');
Expand Down
39 changes: 39 additions & 0 deletions app/Models/AccessoryCheckout.php
@@ -0,0 +1,39 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AccessoryCheckout extends Model
{

protected $guarded = 'id';
protected $table = 'accessories_users';

protected $fillable = [
'assigned_to',
];

/**
* Establishes the checked out accessory -> accessories relationship
*
* @author G. Martinez
* @since [v6.4]
*/
public function accessory(): BelongsTo
{
return $this->belongsTo(Accessory::class, 'accessory_id')->withTrashed();
}

/**
* Establishes the checked out accessory -> assignee relationship
*
* @author G. Martinez
* @since [v6.4]
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'assigned_to')->withTrashed();
}
}
1 change: 1 addition & 0 deletions app/Models/Setting.php
Expand Up @@ -355,6 +355,7 @@ public static function getLdapSettings(): Collection
'ldap_phone_field',
'ldap_jobtitle',
'ldap_manager',
'ldap_company',
'ldap_country',
'ldap_location',
])->first()->getAttributes();
Expand Down
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddLdapCompanyToSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('settings', function (Blueprint $table) {
$table->string('ldap_company')->after('ldap_jobtitle')->nullable()->default(null);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('settings', function (Blueprint $table) {
$table->dropColumn('ldap_company');

});
}
}
1 change: 1 addition & 0 deletions resources/lang/en-US/admin/settings/general.php
Expand Up @@ -104,6 +104,7 @@
'ldap_phone' => 'LDAP Telephone Number',
'ldap_jobtitle' => 'LDAP Job Title',
'ldap_country' => 'LDAP Country',
'ldap_company' => 'LDAP Company',
'ldap_pword' => 'LDAP Bind Password',
'ldap_basedn' => 'Base Bind DN',
'ldap_filter' => 'LDAP Filter',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en-US/general.php
Expand Up @@ -67,6 +67,7 @@
'clear_selection' => 'Clear Selection',
'companies' => 'Companies',
'company' => 'Company',
'company_check' => 'There are :count items assigned to users outside of their company.',
'component' => 'Component',
'components' => 'Components',
'complete' => 'Complete',
Expand Down Expand Up @@ -209,6 +210,7 @@
'no_results' => 'No Results.',
'no' => 'No',
'notes' => 'Notes',
'notifications_clear' => 'There are no notifications at the moment.',
'order_number' => 'Order Number',
'only_deleted' => 'Only Deleted Assets',
'page_menu' => 'Showing _MENU_ items',
Expand Down
41 changes: 36 additions & 5 deletions resources/views/layouts/default.blade.php
Expand Up @@ -266,19 +266,24 @@
@can('admin')
@if ($snipeSettings->show_alerts_in_menu=='1')
<!-- Tasks: style can be found in dropdown.less -->
<?php $alert_items = Helper::checkLowInventory(); ?>
<?php $alert_items = Helper::checkLowInventory(); $company_items = Helper::checkUserCompanyAssets();?>

<li class="dropdown tasks-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="far fa-flag" aria-hidden="true"></i>
<span class="sr-only">{{ trans('general.alerts') }}</span>
@if (count($alert_items))
<span class="label label-danger">{{ count($alert_items) }}</span>
@if (count($alert_items) || count($company_items))
<span class="label label-danger">{{ count($alert_items) + count($company_items) }}</span>
@endif
</a>
<ul class="dropdown-menu">
<li class="header">{{ trans('general.quantity_minimum', array('count' => count($alert_items))) }}</li>
<li>
@if(!empty($alert_items))
<li class="header">{{ trans('general.quantity_minimum', array('count' => count($alert_items))) }}</li>
@endif
@if(empty($alert_items) && empty($company_items))
<li class="header">{{ trans('general.notifications_clear') }}</li>
@endif
<li>
<!-- inner menu: contains the actual data -->
<ul class="menu">

Expand Down Expand Up @@ -309,6 +314,32 @@
{{-- <li class="footer">
<a href="#">{{ trans('general.tasks_view_all') }}</a>
</li> --}}
@if(!empty($company_items))
<li class="header">{{ trans('general.company_check', array('count' => count($company_items))) }}</li>
@endif
<li>
<!-- inner menu: contains the actual data -->
<ul class="menu">

@for($i = 0; count($company_items) > $i; $i++)

<li><!-- Task item -->
<a href="{{route($company_items[$i]['type'].'.show', $company_items[$i]['id'])}}">
<div style="white-space: nowrap; overflow: hidden;"> <h2 class="task_menu">{{$company_items[$i]['company'].": ".$company_items[$i]['name']}}
<div style="clear:both;">
<small>{{trans('general.assigned_to', array('name' => $company_items[$i]['user'])) }}</small>
</div>
<div style="clear:both;">
<small class="pull-left">({{$company_items[$i]['user_company']}}) </small>
</div>
</h2>
</div>
</a>
</li>
<!-- end task item -->
@endfor
</ul>
</li>
</ul>
</li>
@endcan
Expand Down
13 changes: 13 additions & 0 deletions resources/views/settings/ldap.blade.php
Expand Up @@ -500,6 +500,19 @@
@endif
</div>
</div>
<!-- LDAP Company -->
<div class="form-group {{ $errors->has('ldap_company') ? 'error' : '' }}">
<div class="col-md-3">
{{ Form::label('ldap_company', trans('admin/settings/general.ldap_company')) }}
</div>
<div class="col-md-8">
{{ Form::text('ldap_company', Request::old('ldap_company', $setting->ldap_company), ['class' => 'form-control','placeholder' => trans('general.example') .'company', $setting->demoMode]) }}
{!! $errors->first('ldap_company', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
@if (config('app.lock_passwords')===true)
<p class="text-warning"><i class="fas fa-lock" aria-hidden="true"></i> {{ trans('general.feature_disabled') }}</p>
@endif
</div>
</div>
<!-- LDAP Location -->
<div class="form-group {{ $errors->has('ldap_location') ? 'error' : '' }}">
<div class="col-md-3">
Expand Down