Skip to content

Commit

Permalink
Merge pull request #3 from jc0b/dev
Browse files Browse the repository at this point in the history
Big change update (Laravel, Python, Passport integration)
  • Loading branch information
jc0b authored Feb 7, 2023
2 parents 97cffef + ae6651f commit cff94a6
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 180 deletions.
110 changes: 64 additions & 46 deletions kandji_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* @package munkireport
* @author jc0b
**/
use munkireport\models\MRModel as Eloquent;

use Illuminate\Support\Facades\DB;

class kandji_controller extends Module_controller
{
public function __construct()
Expand Down Expand Up @@ -35,45 +39,36 @@ public function admin()
* Get Kandji version for widget
*
* @return void
* @author tuxudo
* @author jc0b
**/
public function get_kandji_version()
{
$sql = "SELECT COUNT(CASE WHEN kandji_agent_version <> '' AND kandji_agent_version IS NOT NULL THEN 1 END) AS count, kandji_agent_version
FROM kandji
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter()."
GROUP BY kandji_agent_version
ORDER BY count DESC";

$queryobj = new Kandji_model;
jsonView($queryobj->query($sql));
$kandji_version_data = Kandji_model::selectRaw("COALESCE(SUM(CASE WHEN kandji_agent_version IS NOT NULL THEN 1 END), 0) AS count, kandji_agent_version")->filter()->groupBy('kandji_agent_version')->orderBy('count', 'desc')->get()->toArray();
$obj = new View();
$obj->view('json', array('msg' => $kandji_version_data));
}


/**
* REST API for retrieving last checkin data for widget
*
* @author tuxudo
* @author jc0b
**/
public function get_last_checkin()
{
$currentdate = date_timestamp_get(date_create());
$week = $currentdate - 604800;
$month = $currentdate - 2592000;

$sql = "SELECT COUNT( CASE WHEN ".$month." >= last_check_in THEN 1 END) AS red,
COUNT( CASE WHEN ".$week." >= last_check_in AND last_check_in > ".$month." THEN 1 END) AS yellow,
COUNT( CASE WHEN last_check_in > ".$week." AND last_check_in > 0 THEN 1 END) AS green
FROM kandji
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter();

$queryobj = new Kandji_model();
foreach($queryobj->query($sql)[0] as $label => $value){
$out[] = ['label' => $label, 'count' => $value];
}
jsonView($out);
$currentdate = date_timestamp_get(date_create());
$week = $currentdate - 604800;
$month = $currentdate - 2592000;

$checkin_data = Kandji_model::selectRaw("COALESCE(SUM(CASE WHEN last_check_in <= $month THEN 1 END), 0) AS red,
COALESCE(SUM(CASE WHEN last_check_in <= $week AND last_check_in > $month THEN 1 END), 0) AS yellow,
COALESCE(SUM(CASE WHEN last_check_in > $week AND last_check_in > 0 THEN 1 END), 0) AS green")
->filter()
->first()
->toLabelCount();

$obj = new View();
$obj->view('json', array('msg' => $checkin_data));
}

/**
Expand All @@ -88,34 +83,55 @@ public function pull_all_kandji_data($incoming_serial = '')
// Returns either a list of all serial numbers in MunkiReport OR
// a JSON of what serial number was just ran with the status of the run
if ( $incoming_serial == ''){
// APP_ROOT is set in index.php
require_once(APP_ROOT.'vendor/munkireport/machine/machine_model.php');
// Get all the serial numbers in an object
$machine = new Kandji_model();
$filter = get_machine_group_filter();

$sql = "SELECT machine.serial_number
FROM machine
LEFT JOIN reportdata USING (serial_number)
$filter";

$machinedata = Machine_model::selectRaw("machine.serial_number")->filter()->get()->toArray();
// Loop through each serial number for processing
$out = array();
foreach ($machine->query($sql) as $serialobj) {
$out[] = $serialobj->serial_number;
foreach ($machinedata as $serialobj) {
$out[] = $serialobj['serial_number'];
}
jsonView($out);
$obj = new View();
$obj->view('json', array('msg' => $out));
} else {

$kandji = new Kandji_model($incoming_serial);
$kandji_status = $kandji->run_kandji_stats();
$kandji = new Kandji_model();
$kandji->serial_number = $incoming_serial;
$kandji->kandji_id = 0;
$kandji_status = $this->run_kandji_stats($kandji);

// Check if machine exists in Kandji
if ($kandji_status->rs['kandji_id'] == 0 ){
if ($kandji->kandji_id == 0 ){
$out = array("serial"=>$incoming_serial,"status"=>"Machine not found in Kandji!");
} else {
$out = array("serial"=>$incoming_serial,"status"=>"Machine processed");
}
jsonView($out);
$obj = new View();
$obj->view('json', array('msg' => $out));
}
}

/**
* Get Kandji data
*
* @return void
* @author jc0b
**/
function run_kandji_stats(&$kandji_model)
{
$module_dir = dirname(__FILE__);
// Check if we should enable Kandji lookup
if (conf('kandji_enable')) {
// Load Kandji helper
require_once($module_dir.'/lib/kandji_helper.php');
$kandji_helper = new munkireport\module\kandji\kandji_helper;
$kandji_helper->pull_kandji_data($kandji_model);
// ^^ Comment and uncomment to turn off and on
}

return $this;
}

/**
Expand All @@ -127,8 +143,9 @@ public function pull_all_kandji_data($incoming_serial = '')
public function recheck_kandji($serial = '')
{
if (authorized_for_serial($serial)) {
$kandji = new Kandji_model($serial);
$kandji->run_kandji_stats();
$kandji = new Kandji_model();
$kandji->serial_number = $serial;
$this->run_kandji_stats($kandji);
}

redirect("clients/detail/$serial#tab_kandji-tab");
Expand All @@ -140,8 +157,9 @@ public function recheck_kandji($serial = '')
* @param string $serial serial number
**/
public function get_data($serial_number = '')
{
$kandji = new Kandji_model($serial_number);
jsonView($kandji->rs);
{
$machinedata = Kandji_model::select("kandji.*")->where("kandji.serial_number", $serial_number)->filter()->get();
$obj = new View();
$obj->view('json', array('msg' => $machinedata[0]));
}
} // End class kandji_module
85 changes: 22 additions & 63 deletions kandji_model.php
Original file line number Diff line number Diff line change
@@ -1,68 +1,27 @@
<?php

class Kandji_model extends \Model {

protected $error = '';
protected $module_dir;

public function __construct($serial = '')
{
parent::__construct('id', 'kandji'); // Primary key, tablename
$this->rs['id'] = '';
$this->rs['serial_number'] = $serial;
$this->rs['kandji_id'] = 0;
$this->rs['name'] = '';
$this->rs['kandji_agent_version'] = '';
$this->rs['asset_tag'] = '';
$this->rs['last_check_in'] = 0;
$this->rs['last_enrollment'] = 0;
$this->rs['first_enrollment'] = 0;
$this->rs['blueprint_id'] = '';
$this->rs['blueprint_name'] = '';
$this->rs['realname'] = '';
$this->rs['email_address'] = '';

use munkireport\models\MRModel as Eloquent;

if ($serial) {
$this->retrieve_record($serial);
}

$this->serial_number = $serial;

$this->module_dir = dirname(__FILE__);

// Add local config
configAppendFile(__DIR__ . '/config.php');
}
class Kandji_model extends Eloquent
{
protected $table = 'kandji';

/**
* Get Kandji data
*
* @return void
* @author jc0b
**/
public function run_kandji_stats()
{
// Check if we should enable Kandji lookup
if (conf('kandji_enable')) {
// Load Kandji helper
require_once($this->module_dir.'/lib/kandji_helper.php');
$kandji_helper = new munkireport\module\kandji\kandji_helper;
$kandji_helper->pull_kandji_data($this);
// ^^ Comment and uncomment to turn off and on
}

return $this;
}

/**
* Process method, is called by the client
*
* @return void
* @author jc0b
**/
public function process()
{
$this->run_kandji_stats();
}
protected $hidden = ['id', 'serial_number'];

protected $fillable = [
'serial_number',
'kandji_id',
'name',
'kandji_agent_version',
'asset_tag',
'last_check_in',
'last_enrollment',
'first_enrollment',
'blueprint_id',
'blueprint_name',
'realname',
'email_address',
'passport_enabled',
'passport_users',
];
}
73 changes: 73 additions & 0 deletions kandji_processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* Kandji processor class
*
* @package munkireport
* @author jc0b
**/

use CFPropertyList\CFPropertyList;
use munkireport\processors\Processor;

class Kandji_processor extends Processor
{
public function run($plist)
{
if ( ! $plist){
throw new Exception("Error Processing Request: No property list found", 1);
}
configAppendFile(__DIR__ . '/config.php');
$module_dir = dirname(__FILE__);

$parser = new CFPropertyList();
$parser->parse($plist, CFPropertyList::FORMAT_XML);
$mylist = $parser->toArray();
// Retrieve Kandji MR record (if existing)
try {
$model = Kandji_model::select()
->where('serial_number', $this->serial_number)
->firstOrFail();
} catch (\Throwable $th) {
$model = new Kandji_model();
}

// Check if we should enable Kandji lookup
if (conf('kandji_enable')) {
// Load Kandji helper
require_once($module_dir.'/lib/kandji_helper.php');
$kandji_helper = new munkireport\module\kandji\kandji_helper;
$json = $kandji_helper->pull_kandji_data($model);

// Transpose Kandji API output into Kandji model
// General section
$mylist['name'] = $json[0]->device_name;
$mylist['asset_tag'] = $json[0]->asset_tag;
$mylist['blueprint_id'] = $json[0]->blueprint_id;
$mylist['blueprint_name'] = $json[0]->blueprint_name;
$mylist['last_check_in'] = $this->convert_time_to_epoch($json[0]->last_check_in);
$mylist['last_enrollment'] = $this->convert_time_to_epoch($json[0]->last_enrollment);
$mylist['first_enrollment'] = $this->convert_time_to_epoch($json[0]->first_enrollment);

// Location section
$mylist['realname'] = $json[0]->user->name;
$mylist['email_address'] = $json[0]->user->email;

}

$model->fill($mylist)->save();
}

/**
* Convert Kandji timestamps to epochs
*
* @return Unix epoch
* @author jc0b
*
**/
private function convert_time_to_epoch($date)
{
$dt = new \DateTime($date);
return $dt->getTimestamp();
}
}
Loading

0 comments on commit cff94a6

Please sign in to comment.