Skip to content

Commit

Permalink
Merge branch 'master' into bug/history-notification-time-format-6980
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Fuhr committed Sep 23, 2014
2 parents e784aa3 + 2ff932f commit 07a4b25
Show file tree
Hide file tree
Showing 16 changed files with 612 additions and 94 deletions.
29 changes: 11 additions & 18 deletions library/Icinga/Authentication/Manager.php
Expand Up @@ -113,30 +113,32 @@ public function persistCurrentUser()
}

/**
* Tries to authenticate the user with the current session
* Try to authenticate the user with the current session
*
* Authentication for externally-authenticated users will be revoked if the username changed or external
* authentication is no longer in effect
*/
public function authenticateFromSession()
{
$this->user = Session::getSession()->get('user');

if ($this->user !== null && $this->user->isRemoteUser() === true) {
list($originUsername, $field) = $this->user->getRemoteUserInformation();
if (array_key_exists($field, $_SERVER) && $_SERVER[$field] !== $originUsername) {
if (! array_key_exists($field, $_SERVER) || $_SERVER[$field] !== $originUsername) {
$this->removeAuthorization();
}
}
}

/**
* Returns true when the user is currently authenticated
* Whether the user is authenticated
*
* @param Boolean $ignoreSession Set to true to prevent authentication by session
* @param bool $ignoreSession True to prevent session authentication
*
* @return bool
*/
public function isAuthenticated($ignoreSession = false)
{
if ($this->user === null && !$ignoreSession) {
if ($this->user === null && ! $ignoreSession) {
$this->authenticateFromSession();
}
return is_object($this->user);
Expand All @@ -145,25 +147,16 @@ public function isAuthenticated($ignoreSession = false)
/**
* Whether an authenticated user has a given permission
*
* This is true if the user owns this permission, false if not.
* Also false if there is no authenticated user
*
* TODO: I'd like to see wildcard support, e.g. module/*
*
* @param string $permission Permission name
* @return bool
*
* @return bool True if the user owns the given permission, false if not or if not authenticated
*/
public function hasPermission($permission)
{
if (! $this->isAuthenticated()) {
return false;
}
foreach ($this->user->getPermissions() as $p) {
if ($p === $permission) {
return true;
}
}
return false;
return $this->user->can($permission);
}

/**
Expand Down
43 changes: 37 additions & 6 deletions library/Icinga/User.php
Expand Up @@ -187,23 +187,27 @@ public function isMemberOf($group)
}

/**
* Return permission information for this user
* Get the user's permissions
*
* @return array
* @return array
*/
public function getPermissions()
{
return $this->permissions;
}

/**
* Setter for permissions
* Set the user's permissions
*
* @param array $permissions
*
* @param array $permissions
* @return $this
*/
public function setPermissions(array $permissions)
{
$this->permissions = $permissions;
natcasesort($permissions);
$this->permissions = array_combine($permissions, $permissions);
return $this;
}

/**
Expand Down Expand Up @@ -442,6 +446,33 @@ public function getRemoteUserInformation()
*/
public function isRemoteUser()
{
return (count($this->remoteUserInformation)) ? true : false;
return ! empty($this->remoteUserInformation);
}

/**
* Whether the user has a given permission
*
* @param string $permission
*
* @return bool
*/
public function can($permission)
{
if (isset($this->permissions['*']) || isset($this->permissions[$permission])) {
return true;
}
foreach ($this->permissions as $permitted) {
$wildcard = strpos($permitted, '*');
if ($wildcard !== false) {
if (substr($permission, 0, $wildcard) === substr($permitted, 0, $wildcard)) {
return true;
} else {
if ($permission === $permitted) {
return true;
}
}
}
}
return false;
}
}
3 changes: 2 additions & 1 deletion library/Icinga/Web/JavaScript.php
Expand Up @@ -26,7 +26,8 @@ class JavaScript
'js/icinga/behavior/tooltip.js',
'js/icinga/behavior/sparkline.js',
'js/icinga/behavior/tristate.js',
'js/icinga/behavior/navigation.js'
'js/icinga/behavior/navigation.js',
'js/icinga/behavior/form.js'
);

protected static $vendorFiles = array(
Expand Down
68 changes: 48 additions & 20 deletions library/Icinga/Web/Widget/Chart/HistoryColorGrid.php
Expand Up @@ -28,29 +28,32 @@ class HistoryColorGrid extends AbstractWidget {
private $maxValue = 1;

private $start = null;

private $end = null;

private $data = array();

private $color;
public $opacity = 1.0;

public function __construct($color = '#51e551') {
public function __construct($color = '#51e551', $start = null, $end = null) {
$this->setColor($color);
if (isset($start)) {
$this->start = $this->tsToDateStr($start);
}
if (isset($end)) {
$this->end = $this->tsToDateStr($end);
}
}

/**
* Set the displayed data-set
*
* @param $data array The values to display.
* properties for each entry:
* @param $events array The history events to display as an array of arrays:
* value: The value to display
* caption: The caption on mouse-over
* url: The url to open on click.
*/
public function setData(array $data)
public function setData(array $events)
{
$this->data = $data;
$this->data = $events;
$start = time();
$end = time();
foreach ($this->data as $entry) {
Expand All @@ -68,8 +71,12 @@ public function setData(array $data)
$start = $time;
}
}
$this->start = $this->tsToDateStr($start);
$this->end = $this->tsToDateStr($end);
if (!isset($this->start)) {
$this->start = $this->tsToDateStr($start);
}
if (!isset($this->end)) {
$this->end = $this->tsToDateStr($end);
}
}

/**
Expand All @@ -82,6 +89,16 @@ public function setColor($color)
$this->color = $color;
}

/**
* Set the used opacity
*
* @param $opacity
*/
public function setOpacity($opacity)
{
$this->opacity = $opacity;
}

/**
* Calculate the color to display for the given value.
*
Expand All @@ -104,16 +121,17 @@ private function calculateColor($value)
*/
private function renderDay($day)
{
if (array_key_exists($day, $this->data)) {
if (array_key_exists($day, $this->data) && $this->data[$day]['value'] > 0) {
$entry = $this->data[$day];
return'<a ' .
'style="background-color:' . $this->calculateColor($entry['value']) . ';" ' .
'style="background-color:' . $this->calculateColor($entry['value']) . '; '
. ' opacity: ' . $this->opacity . ';"' .
'title="' . $entry['caption'] . '" ' .
'href="' . $entry['url'] . '"' .
'>&nbsp;</a>';
} else {
return '<a ' .
'style="background-color:' . $this->calculateColor(0) . ';" ' .
'style="background-color:' . $this->calculateColor(0) . '; ' . ' opacity: ' . $this->opacity . ';" ' .
'title="No entries for ' . $day . '" ' .
'></a>';
}
Expand All @@ -130,13 +148,14 @@ private function renderHorizontal($grid)
{
$weeks = $grid['weeks'];
$months = $grid['months'];
$years = $grid['years'];
$html = '<table class="historycolorgrid">';
$html .= '<tr><th></th>';
$old = -1;
foreach ($months as $month) {
foreach ($months as $week => $month) {
if ($old !== $month) {
$old = $month;
$txt = $this->monthName($month);
$txt = $this->monthName($month, $years[$week]);
} else {
$txt = '';
}
Expand All @@ -157,6 +176,7 @@ private function renderHorizontal($grid)
*/
private function renderVertical($grid)
{
$years = $grid['years'];
$weeks = $grid['weeks'];
$months = $grid['months'];
$html = '<table class="historycolorgrid">';
Expand All @@ -176,7 +196,7 @@ private function renderVertical($grid)
}
if ($old !== $months[$index]) {
$old = $months[$index];
$txt = $this->monthName($old);
$txt = $this->monthName($old, $years[$index]);
} else {
$txt = '';
}
Expand Down Expand Up @@ -220,6 +240,7 @@ private function createGrid()
$weeks = array(array());
$week = 0;
$months = array();
$years = array();
$start = strtotime($this->start);
$year = intval(date('Y', $start));
$month = intval(date('n', $start));
Expand All @@ -232,6 +253,7 @@ private function createGrid()

$date = $this->toDateStr($day, $month, $year);
$weeks[0][$weekday] = $date;
$years[0] = $year;
$months[0] = $month;
while ($date !== $this->end) {
$day++;
Expand All @@ -242,6 +264,7 @@ private function createGrid()
// PRESENT => The last day of week determines the month
if ($this->weekFlow === self::CAL_GROW_INTO_PRESENT) {
$months[$week] = $month;
$years[$week] = $year;
}
$week++;
}
Expand All @@ -257,21 +280,25 @@ private function createGrid()
// PAST => The first day of each week determines the month
if ($this->weekFlow === self::CAL_GROW_INTO_PAST) {
$months[$week] = $month;
$years[$week] = $year;
}
}
$date = $this->toDateStr($day, $month, $year);
$weeks[$week][$weekday] = $date;
};
$years[$week] = $year;
$months[$week] = $month;
if ($this->weekFlow == self::CAL_GROW_INTO_PAST) {
return array(
'weeks' => array_reverse($weeks),
'months' => array_reverse($months)
'months' => array_reverse($months),
'years' => array_reverse($years)
);
}
return array(
'weeks' => $weeks,
'months' => $months
'months' => $months,
'years' => $years
);
}

Expand All @@ -282,9 +309,10 @@ private function createGrid()
*
* @return string The
*/
private function monthName($month)
private function monthName($month, $year)
{
$dt = DateTimeFactory::create('2000-' . $month . '-01');
// TODO: find a way to render years without messing up the layout
$dt = DateTimeFactory::create($year . '-' . $month . '-01');
return $dt->format('M');
}

Expand Down

0 comments on commit 07a4b25

Please sign in to comment.