new: [UI] Find org images also by uuid and support SVG images

pull/6730/head
Jakub Onderka 2020-12-10 13:45:56 +01:00
parent f0877f53a5
commit aba8317d89
17 changed files with 128 additions and 117 deletions

View File

@ -83,20 +83,20 @@ class AttributesController extends AppController
}
return $this->RestResponse->viewData($attributes, $this->response->type());
}
$orgTable = $this->Attribute->Event->Orgc->find('list', array(
'fields' => array('Orgc.id', 'Orgc.name')
));
$orgTable = $this->Attribute->Event->Orgc->find('all', [
'fields' => ['Orgc.id', 'Orgc.name', 'Orgc.uuid'],
]);
$orgTable = Hash::combine($orgTable, '{n}.Orgc.id', '{n}.Orgc');
foreach ($attributes as &$attribute) {
if (isset($orgTable[$attribute['Event']['orgc_id']])) {
$attribute['Event']['Orgc'] = [
'id' => $attribute['Event']['orgc_id'],
'name' => $orgTable[$attribute['Event']['orgc_id']],
];
$attribute['Event']['Orgc'] = $orgTable[$attribute['Event']['orgc_id']];
}
}
list($attributes, $sightingsData) = $this->__searchUI($attributes);
$this->set('sightingsData', $sightingsData);
$this->set('orgTable', $orgTable);
$this->set('orgTable', array_column($orgTable, 'name', 'id'));
$this->set('shortDist', $this->Attribute->shortDist);
$this->set('attributes', $attributes);
$this->set('attrDescriptions', $this->Attribute->fieldDescriptions);
@ -1591,21 +1591,16 @@ class AttributesController extends AppController
);
$attributes = $this->paginate();
$orgTable = $this->Attribute->Event->Orgc->find('list', array(
'fields' => ['Orgc.id', 'Orgc.name'],
));
$orgTable = $this->Attribute->Event->Orgc->find('all', [
'fields' => ['Orgc.id', 'Orgc.name', 'Orgc.uuid'],
]);
$orgTable = Hash::combine($orgTable, '{n}.Orgc.id', '{n}.Orgc');
foreach ($attributes as &$attribute) {
if (isset($orgTable[$attribute['Event']['orgc_id']])) {
$attribute['Event']['Orgc'] = [
'id' => $attribute['Event']['orgc_id'],
'name' => $orgTable[$attribute['Event']['orgc_id']],
];
$attribute['Event']['Orgc'] = $orgTable[$attribute['Event']['orgc_id']];
}
if (isset($orgTable[$attribute['Event']['org_id']])) {
$attribute['Event']['Org'] = [
'id' => $attribute['Event']['org_id'],
'name' => $orgTable[$attribute['Event']['org_id']],
];
$attribute['Event']['Org'] = $orgTable[$attribute['Event']['org_id']];
}
}
if ($this->_isRest()) {
@ -1634,7 +1629,7 @@ class AttributesController extends AppController
}
}
}
$this->set('orgTable', $orgTable);
$this->set('orgTable', array_column($orgTable, 'name', 'id'));
$this->set('filters', $filters);
$this->set('attributes', $attributes);
$this->set('isSearch', 1);

View File

@ -128,14 +128,7 @@ class OrganisationsController extends AppController
}
}
if ($this->Organisation->save($this->request->data)) {
if (isset($this->request->data['Organisation']['logo']['size']) && $this->request->data['Organisation']['logo']['size'] > 0 && $this->request->data['Organisation']['logo']['error'] == 0) {
$filename = basename($this->Organisation->id . '.png');
if (preg_match("/^[0-9a-z\-\_\.]*\.(png)$/i", $filename)) {
if (!empty($this->request->data['Organisation']['logo']['tmp_name']) && is_uploaded_file($this->request->data['Organisation']['logo']['tmp_name'])) {
$result = move_uploaded_file($this->request->data['Organisation']['logo']['tmp_name'], APP . 'webroot/img/orgs/' . $filename);
}
}
}
$this->__uploadLogo($this->Organisation->id);
if ($this->_isRest()) {
$org = $this->Organisation->find('first', array(
'conditions' => array('Organisation.id' => $this->Organisation->id),
@ -204,14 +197,7 @@ class OrganisationsController extends AppController
}
$this->request->data['Organisation']['id'] = $id;
if ($this->Organisation->save($this->request->data)) {
if (isset($this->request->data['Organisation']['logo']['size']) && $this->request->data['Organisation']['logo']['size'] > 0 && $this->request->data['Organisation']['logo']['error'] == 0) {
$filename = basename($this->request->data['Organisation']['id'] . '.png');
if (preg_match("/^[0-9a-z\-\_\.]*\.(png)$/i", $filename)) {
if (!empty($this->request->data['Organisation']['logo']['tmp_name']) && is_uploaded_file($this->request->data['Organisation']['logo']['tmp_name'])) {
$result = move_uploaded_file($this->request->data['Organisation']['logo']['tmp_name'], APP . 'webroot/img/orgs/' . $filename);
}
}
}
$this->__uploadLogo($this->Organisation->id);
if ($this->_isRest()) {
$org = $this->Organisation->find('first', array(
'conditions' => array('Organisation.id' => $this->Organisation->id),
@ -475,4 +461,25 @@ class OrganisationsController extends AppController
$this->render('ajax/merge');
}
}
/**
* @return bool
*/
private function __uploadLogo($orgId)
{
if (!isset($this->request->data['Organisation']['logo']['size'])) {
return false;
}
$logo = $this->request->data['Organisation']['logo'];
if ($logo['size'] > 0 && $logo['error'] == 0) {
$extension = pathinfo($logo['name'], PATHINFO_EXTENSION);
$filename = $orgId . '.' . ($extension === 'svg' ? 'svg' : 'png');
if (!empty($logo['tmp_name']) && is_uploaded_file($logo['tmp_name'])) {
return move_uploaded_file($logo['tmp_name'], APP . 'webroot/img/orgs/' . $filename);
}
}
return false;
}
}

View File

@ -1,7 +1,9 @@
<?php
App::uses('AppController', 'Controller');
/**
* @property Thread $Thread
*/
class ThreadsController extends AppController
{
public $components = array(
@ -94,16 +96,16 @@ class ThreadsController extends AppController
}
if ($thread_id) {
$this->paginate = array(
'limit' => 10,
'conditions' => array('Post.thread_id' => $thread_id),
'contain' => array(
'User' => array(
'fields' => array('User.email', 'User.id'),
'Organisation' => array(
'fields' => array('id', 'name')
),
),
'limit' => 10,
'conditions' => array('Post.thread_id' => $thread_id),
'contain' => array(
'User' => array(
'fields' => array('User.email', 'User.id'),
'Organisation' => array(
'fields' => array('id', 'uuid', 'name')
),
),
),
);
if ($this->_isRest()) {
$posts = $this->Thread->Post->find('all', array(
@ -114,7 +116,14 @@ class ThreadsController extends AppController
$posts = $this->paginate('Post');
}
foreach ($posts as $k => $post) {
$posts[$k]['Post']['org_name'] = empty($post['User']['id']) ? 'Deactivated user' : $post['User']['Organisation']['name'];
if (!empty($post['User']['id'])) {
$posts[$k]['Post']['org_id'] = $post['User']['Organisation']['id'];
$posts[$k]['Post']['org_uuid'] = $post['User']['Organisation']['uuid'];
$posts[$k]['Post']['org_name'] = $post['User']['Organisation']['name'];
} else {
$posts[$k]['Post']['org_name'] = 'Deactivated user'; // to keep BC
}
if ($this->_isSiteAdmin() || $this->Auth->user('org_id') == $post['User']['org_id']) {
$posts[$k]['Post']['user_email'] = empty($post['User']['id']) ? 'Unavailable' : $post['User']['email'];
}

View File

@ -15,15 +15,15 @@ class UsersController extends AppController
);
public $paginate = array(
'limit' => 60,
'recursive' => -1,
'order' => array(
'Organisation.name' => 'ASC'
),
'contain' => array(
'Organisation' => array('id', 'name'),
'Role' => array('id', 'name', 'perm_auth', 'perm_site_admin')
)
'limit' => 60,
'recursive' => -1,
'order' => array(
'Organisation.name' => 'ASC'
),
'contain' => array(
'Organisation' => array('id', 'uuid', 'name'),
'Role' => array('id', 'name', 'perm_auth', 'perm_site_admin')
)
);
public $helpers = array('Js' => array('Jquery'));

View File

@ -12,7 +12,7 @@
<tbody>
<tr>
<td rowspan="2" style="border-right: 1px solid #ddd; padding-right: 2px; min-width: 24px; max-width: 24px; overflow: hidden; font-size: xx-small; text-overflow: ellipsis;" title="<?php echo h($related['Orgc']['name']); ?>">
<?php echo $this->OrgImg->getOrgImg(array('name' => $related['Orgc']['name'], 'id' => $related['Orgc']['id'], 'size' => 24)); ?>
<?= $this->OrgImg->getOrgLogo($related['Orgc'], 24) ?>
</td>
<td style="padding-left: 2px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; max-width: 410px;">
<a title="<?php echo h($related['info']); ?>" href="<?php echo h($href_url)?>">

View File

@ -94,9 +94,9 @@ $quickEdit = function($fieldName) use ($editScope, $object, $event) {
if (!empty($extended)):
if ($object['event_id'] != $event['Event']['id']):
$extensionOrg = $event['extensionEvents'][$object['event_id']]['Orgc'];
echo $this->OrgImg->getOrgImg(array('name' => $extensionOrg['name'], 'id' => $extensionOrg['id'], 'size' => 24));
echo $this->OrgImg->getOrgLogo($extensionOrg['name'], 24);
else:
echo $this->OrgImg->getOrgImg(array('name' => $event['Orgc']['name'], 'id' => $event['Orgc']['id'], 'size' => 24));
echo $this->OrgImg->getOrgLogo($event['Orgc'], 24);
endif;
endif;
?>

View File

@ -82,17 +82,12 @@
</td>
<?php if (Configure::read('MISP.showorg') || $isAdmin): ?>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl . "/events/index/searchorg:" . $event['Orgc']['id'];?>'">
<?php
echo $this->OrgImg->getOrgImg(array('name' => $event['Orgc']['name'], 'id' => $event['Orgc']['id'], 'size' => 24));
?>
<?= $this->OrgImg->getOrgLogo($event['Orgc'], 24) ?>
</td>
<?php endif;?>
<?php if ($isSiteAdmin || (Configure::read('MISP.showorgalternate') && Configure::read('MISP.showorg'))): ?>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl . "/events/index/searchorg:" . $event['Org']['id'];?>'">
<?php
echo $this->OrgImg->getOrgImg(array('name' => $event['Org']['name'], 'id' => $event['Org']['id'], 'size' => 24));
?>
&nbsp;
<?= $this->OrgImg->getOrgLogo($event['Org'], 24) ?>
</td>
<?php endif; ?>
<td style="width:30px;">

View File

@ -29,13 +29,9 @@
<div>
<table style="width:100%">
<tr>
<td>
<?php
echo __('Date: ') . h($post['date_created']);
?>
</td>
<td><?= __('Date: ') . h($post['date_created']) ?></td>
<td style="text-align:right">
<a href="#top" class="whitelink">Top</a> |
<a href="#top" class="whitelink"><?= __('Top') ?></a> |
<a href="#message_<?php echo h($post['id']); ?>" class="whitelink">#<?php echo h($post['id'])?></a>
</td>
</tr>
@ -46,8 +42,8 @@
<tr>
<td class="discussionBoxTD discussionBoxTDMid discussionBoxTDMidLeft">
<?php
if (isset($post['org_name'])) {
echo $this->OrgImg->getOrgImg(array('name' => $post['org_name'], 'size' => 48));
if (isset($post['org_id'])) {
echo $this->OrgImg->getOrgLogo(['id' => $post['org_id'], 'name' => $post['org_name'], 'uuid' => $post['org_uuid']], 48);
} else {
echo __('Deactivated user');
}

View File

@ -16,14 +16,7 @@
$i++;
if (!empty($org['id']) || !empty($org['name'])) {
if ($field['fields']['allow_picture']) {
$orgLookupData = [];
foreach (['name', 'id', 'uuid'] as $orgLookupField) {
if (!empty($org[$orgLookupField])) {
$orgLookupData[$orgLookupField] = $org[$orgLookupField];
}
}
$orgLookupData['size'] = 24;
echo $this->OrgImg->getOrgImg($orgLookupData);
echo $this->OrgImg->getOrgLogo($org, 24);
} else {
echo sprintf(
'<a href="%s/organisations/view/%s">%s</a>',
@ -33,13 +26,12 @@
);
}
if ($i < $count) {
echo '<br />';
echo '<br>';
}
} else {
if ($field['fields']['allow_picture']) {
echo $this->OrgImg->getOrgImg(array('name' => $field['fields']['default_org'], 'size' => 24), false, true);
echo $this->OrgImg->getOrgLogo(['name' => $field['fields']['default_org']], 24, false);
}
}
}
}
?>

View File

@ -15,7 +15,7 @@
<?php
if (Configure::read('MISP.showorg') || $isAdmin):
?>
<div style="float:right;"><?php echo $this->OrgImg->getOrgImg(array('name' => $event['Orgc']['name'], 'id' => $event['Orgc']['id'], 'size' => 48)); ?></div>
<div style="float:right;"><?= $this->OrgImg->getOrgLogo($event['Orgc'], 48); ?></div>
<?php
endif;
$title = h($event['Event']['info']);

View File

@ -12,13 +12,7 @@ class OrgImgHelper extends AppHelper
return '';
}
$orgImgName = null;
foreach (['id', 'name'] as $field) {
if (isset($organisation['Organisation'][$field]) && file_exists(self::IMG_PATH . $organisation['Organisation'][$field] . '.png')) {
$orgImgName = $organisation['Organisation'][$field] . '.png';
break;
}
}
$orgImgName = $this->findOrgImage($organisation['Organisation']);
$baseurl = $this->_View->viewVars['baseurl'];
$link = $baseurl . '/organisations/view/' . (empty($organisation['Organisation']['id']) ? h($organisation['Organisation']['name']) : h($organisation['Organisation']['id']));
if ($orgImgName) {
@ -29,15 +23,29 @@ class OrgImgHelper extends AppHelper
}
}
/**
* @param array $organisation
* @param int $size
* @param bool $withLink
* @return string
*/
public function getOrgLogo(array $organisation, $size, $withLink = true)
{
if (isset($organisation['Organisation'])) {
$options = $organisation['Organisation'];
} else {
$options = $organisation;
}
$options['size'] = $size;
return $this->getOrgImg($options, true, !$withLink);
}
/**
* @deprecated
*/
public function getOrgImg($options, $returnData = false, $raw = false)
{
$orgImgName = null;
foreach (['id', 'name'] as $field) {
if (isset($options[$field]) && file_exists(self::IMG_PATH . $options[$field] . '.png')) {
$orgImgName = $options[$field] . '.png';
break;
}
}
$orgImgName = $this->findOrgImage($options);
$baseurl = $this->_View->viewVars['baseurl'];
if ($orgImgName) {
$size = !empty($options['size']) ? $options['size'] : 48;
@ -80,4 +88,22 @@ class OrgImgHelper extends AppHelper
echo $result;
}
}
/**
* @param array $options
* @return string|null
*/
private function findOrgImage(array $options)
{
foreach (['id', 'name', 'uuid'] as $field) {
if (isset($options[$field])) {
foreach (['png', 'svg'] as $extensions) {
if (file_exists(self::IMG_PATH . $options[$field] . '.' . $extensions)) {
return $options[$field] . '.' . $extensions;
}
}
}
}
return null;
}
}

View File

@ -34,7 +34,7 @@
echo $this->Form->input('logo', array(
'error' => array('escape' => false),
'type' => 'file',
'label' => __('Logo (48x48 png)')
'label' => __('Logo (48×48 PNG or SVG)')
));
?>
<div class="clear"></div>

View File

@ -45,7 +45,7 @@
echo $this->Form->input('logo', array(
'error' => array('escape' => false),
'type' => 'file',
'label' => __('Logo (48x48 png)')
'label' => __('Logo (48×48 PNG or SVG)')
));
?>
<div class="clear"></div>

View File

@ -106,9 +106,7 @@ foreach ($orgs as $org): ?>
<tr>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl . "/organisations/view/" . $org['Organisation']['id'];?>'"><?php echo h($org['Organisation']['id']); ?></td>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl . "/organisations/view/" . $org['Organisation']['id'];?>'">
<?php
echo $this->OrgImg->getOrgImg(array('name' => $org['Organisation']['name'], 'id' => $org['Organisation']['id'], 'size' => 24));
?>
<?= $this->OrgImg->getOrgLogo($org, 24) ?>
</td>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl . "/organisations/view/" . $org['Organisation']['id'];?>'"><?php echo h($org['Organisation']['name']); ?></td>
<?php if ($isSiteAdmin): ?>

View File

@ -55,14 +55,7 @@
),
sprintf(
'<div style="float:right;">%s</div>',
$this->OrgImg->getOrgImg(
array(
'name' => $org['Organisation']['name'],
'id' => $org['Organisation']['id'],
'size' => 48
),
true
)
$this->OrgImg->getOrgLogo($org, 48)
)
);
?>

View File

@ -31,7 +31,7 @@ echo $this->element('genericElements/viewMetaTable', ['table_data' => $tableData
if (isset($sg['SharingGroupOrg'])):
?>
<div class="span6">
<b><?php echo __('Organisations');?></b>
<b><?php echo __('Organisations');?></b>
<table class="table table-striped table-hover table-condensed">
<tr>
<th><?= __('Name') ?></th>
@ -42,7 +42,7 @@ echo $this->element('genericElements/viewMetaTable', ['table_data' => $tableData
foreach ($sg['SharingGroupOrg'] as $sgo):
?>
<tr>
<td><a href="<?php echo $baseurl; ?>/organisations/view/<?php echo h($sgo['Organisation']['id']); ?>"><?php echo h($sgo['Organisation']['name']); ?></a></td>
<td><?= $this->OrgImg->getNameWithImg($sgo) ?></td>
<td><span class="<?= $sgo['Organisation']['local'] ? 'fas fa-check' : 'fas fa-times' ?>"></span></td>
<td><span class="<?= $sgo['extend'] ? 'fas fa-check' : 'fas fa-times' ?>"></span></td>
</tr>
@ -56,7 +56,7 @@ echo $this->element('genericElements/viewMetaTable', ['table_data' => $tableData
if (!$sg['SharingGroup']['roaming']):
?>
<div class="span6">
<b>Instances</b>
<b>Instances</b>
<table class="table table-striped table-hover table-condensed">
<tr>
<th><?= __('Name') ?></th>

View File

@ -2681,7 +2681,7 @@ a.orgImg {
background-size: 20px;
padding-left: 25px;
padding-top: 3px;
padding-bottom: 2px;
padding-bottom: 3px;
}
.misp-error-container {