Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
feat(*): add demo mode
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Jan 25, 2017
1 parent ef10f23 commit 949f7c2
Show file tree
Hide file tree
Showing 24 changed files with 2,161 additions and 373 deletions.
524 changes: 524 additions & 0 deletions inc/accountvalidation.class.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@link https://github.com/flyvemdm/backend
@link http://www.glpi-project.org/
------------------------------------------------------------------------------
*/
*/

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
Expand Down
73 changes: 73 additions & 0 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,32 @@ public function showForm() {
echo '<td>'. __("No more devices than this quantity are allowed per entity by default (0 = no limitation)", "storkmdm").'</td>';
echo '</tr>';

echo '<tr><th colspan="3">'.__('Demo mode', "storkmdm").'</th></tr>';

echo '<tr class="tab_bg_1">';
echo '<td>'. __("Demo mode", "storkmdm").'</td>';
echo '<td>' . Dropdown::showYesNo('demo_mode', $config['demo_mode'], -1, array('display' => false));
echo '</td>';
echo '<td>'. __("Demo mode enables email validation step when self creating an entity", "storkmdm").'</td>';
echo '</tr>';

echo '<tr class="tab_bg_1">';
echo '<td>'. __("Time limit", "storkmdm").'</td>';
echo '<td>' . Dropdown::showYesNo('demo_time_limit', $config['demo_time_limit'], -1, array('display' => false));
echo '</td>';
echo '<td>'. __("Limit lifetime of a demo account", "storkmdm").'</td>';
echo '</tr>';

echo '<tr><th colspan="3">'.__('Frontend setup', "storkmdm").'</th></tr>';

echo '<tr class="tab_bg_1">';
echo '<td>'. __("Webapp URL", "storkmdm").'</td>';
echo '<td><input type="text" name="webapp_url"' .
'value="'. $config['webapp_url'] .'" />';
echo '</td>';
echo '<td>'. __("URL of the web interface used for management", "storkmdm").'</td>';
echo '</tr>';

echo '<tr class="tab_bg_1">';
echo '<td>'. __("Service's User Token", "storkmdm").'</td>';
echo '<td>' . $apiKey;
Expand Down Expand Up @@ -231,6 +255,22 @@ public static function configUpdate($input) {
}
}
}
if (isset($input['demo_mode'])) {
if ($input['demo_mode'] != '0'
&& (!isset($input['webapp_url']) || empty($input['webapp_url']))) {
Session::addMessageAfterRedirect(__('To enable the demo mode, you must provide the webapp URL !', 'storkmdm', false, ERROR));
unset($input['demo_mode']);
} else {
$config = new static();
if ($input['demo_mode'] == 0) {
$config->resetDemoNotificationSignature();
$config->disableDemoAccountService();
} else {
$config->setDemoNotificationSignature();
$config->enableDemoAccountService();
}
}
}
unset($input['_CACertificateFile']);
unset($input['_tag_CACertificateFile']);
unset($input['CACertificateFile']);
Expand All @@ -252,4 +292,37 @@ public static function undiscloseConfigValue($fields) {
}
return $fields;
}

protected function setDemoNotificationSignature() {
$config = Config::setConfigurationValues('core', [
'mailing_signature' => '',
]);
}

protected function resetDemoNotificationSignature() {
$config = Config::setConfigurationValues('core', [
'mailing_signature' => 'SIGNATURE',
]);
}

protected function enableDemoAccountService() {
$user = new User();
if ($user->getFromDBbyName(self::SERVICE_ACCOUNT_NAME)) {
$user->update(array(
'id' => $user->getID(),
'is_active' => 1
));
}
}

protected function disableDemoAccountService() {
$user = new User();
if ($user->getFromDBbyName(self::SERVICE_ACCOUNT_NAME)) {
$user->update(array(
'id' => $user->getID(),
'is_active' => 1
));
}
}

}
4 changes: 2 additions & 2 deletions inc/entityconfig.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ public static function hook_entity_add(CommonDBTM $item) {
// Create subdirectories for aplications and files
$packagesDir = STORKMDM_PACKAGE_PATH . "/" . $item->getID();
if (!is_dir($packagesDir) && !is_readable($packagesDir)) {
if (! @mkdir($packagesDir, 0770)) {
if (! @mkdir($packagesDir, 0770, true)) {
Toolbox::logInFile("php-errors", "Could not create directory $packagesDir");
// TODO : handle error here
}
}

$filesDir = STORKMDM_FILE_PATH . "/" . $item->getID();
if (!is_dir($filesDir) && !is_readable($filesDir)) {
if (! @mkdir($filesDir, 0770)) {
if (! @mkdir($filesDir, 0770, true)) {
Toolbox::logInFile("php-errors", "Could not create directory $filesDir");
// TODO : handle error here
}
Expand Down
205 changes: 205 additions & 0 deletions inc/notificationtargetaccountvalidation.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?php
/**
LICENSE
Copyright (C) 2016 Teclib'
Copyright (C) 2010-2016 by the FusionInventory Development Team.
This file is part of Flyve MDM Plugin for GLPI.
Flyve MDM Plugin for GLPi is a subproject of Flyve MDM. Flyve MDM is a mobile
device management software.
Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
------------------------------------------------------------------------------
@author Thierry Bugier Pineau
@copyright Copyright (c) 2016 Flyve MDM plugin team
@license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
@link https://github.com/flyvemdm/backend
@link http://www.glpi-project.org/
------------------------------------------------------------------------------
*/

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}

/**
* @since 0.1.33
*/
class PluginStorkmdmNotificationTargetAccountvalidation extends NotificationTarget {

const EVENT_SELF_REGISTRATION = 'plugin_flyvemdm_self_registration';
const EVENT_TRIAL_BEGIN = 'plugin_flyvemdm_trial_begin';
const EVENT_TRIAL_EXPIRATION_REMIND_1 = 'plugin_flyvemdm_trial_remind_1';
const EVENT_TRIAL_EXPIRATION_REMIND_2 = 'plugin_flyvemdm_trial_remind_2';
const EVENT_POST_TRIAL_REMIND = 'plugin_flyvemdm_post_trial';

/**
*
* @param number $nb
* @return translated
*/
static function getTypeName($nb=0) {
return _n('Account validation', 'Account validations', $nb);
}

/**
* Define plugins notification events
* @return Array Events ids => names
*/
public function getEvents() {
return array(
self::EVENT_SELF_REGISTRATION => __('User registration', 'storkmdm')
);
}

/**
* @param NotificationTarget $target
*/
public static function addEvents($target) {
Plugin::loadLang('storkmdm');
$target->events[self::EVENT_SELF_REGISTRATION] = __('User registration', 'storkmdm');
}

/**
* Get available tags for plugins notifications
*/
public function getTags() {
$tagCollection = array(
'storkmdm.registration_url' => __('Account validation URL', 'storkmdm'),
'storkmdm.webapp_url' => __('URL to the web application', 'storkmdm'),
'storkmdm.activation_delay' => __('Account activation delay', 'storkmdm'),
'storkmdm.trial_duration' => __('Duration of a trial account', 'storkmdm'),
'storkmdm.days_remaining' => __('Trial days remaining', 'storkmdm'),
);

foreach ($tagCollection as $tag => $label) {
$this->addTagToList(array('tag' => $tag,
'label' => $label,
'value' => true,
'events' => NotificationTarget::TAG_FOR_ALL_EVENTS));
}

}

/**
* @param NotificationTarget $event
* @param array $options
*/
public static function getAdditionalDatasForTemplate(NotificationTarget $event) {
$signatureDocuments = array_values(Config::getConfigurationValues('storkmdm', [
'social_media_twit',
'social_media_gplus',
'social_media_facebook',
]));

switch ($event->raiseevent) {
case self::EVENT_SELF_REGISTRATION:
$config = Config::getConfigurationValues('storkmdm', array('webapp_url'));
if (isset($event->obj)) {
$accountValidation = $event->obj;
$accountValidationId = $accountValidation->getID();
$validationToken = $accountValidation->getField('validation_pass');
$validationUrl = $config['webapp_url'] . "#!/account/$accountValidationId/validation/$validationToken";

$activationDelay = new DateInterval('P' . $accountValidation->getActivationDelay() . 'D');
$activationDelay = $activationDelay->format('%d');
$activationDelay.= " " . _n('day', 'days', $activationDelay, 'storkmdm');

$trialDuration = new DateInterval('P' . $accountValidation->getTrialDuration() . 'D');
$trialDuration = $trialDuration->format('%d');
$trialDuration.= " " . _n('day', 'days', $trialDuration, 'storkmdm');

// Fill the template
$event->datas['##storkmdm.registration_url##'] = $validationUrl;
$event->datas['##storkmdm.webapp_url##'] = $config['webapp_url'];
$event->datas['##storkmdm.activation_delay##'] = $activationDelay;
$event->datas['##storkmdm.trial_duration##'] = $trialDuration;

$event->obj->documents = $signatureDocuments;
}
break;

case self::EVENT_TRIAL_BEGIN:
$config = Config::getConfigurationValues('storkmdm', array('webapp_url'));
if (isset($event->obj)) {
$event->datas['##storkmdm.webapp_url##'] = $config['webapp_url'];

$event->obj->documents = $signatureDocuments;
}
break;

case self::EVENT_TRIAL_EXPIRATION_REMIND_1:
case self::EVENT_TRIAL_EXPIRATION_REMIND_2:
$config = Config::getConfigurationValues('storkmdm', array('webapp_url'));
if (isset($event->obj)) {
$accountValidation = $event->obj;

// Compute the remaining trial days depending on the first or second reminder
switch ($event->raiseevent) {
case self::EVENT_TRIAL_EXPIRATION_REMIND_1:
$delay = $accountValidation->getReminderDelay(1);
break;

case self::EVENT_TRIAL_EXPIRATION_REMIND_2:
$delay = $accountValidation->getReminderDelay(2);
break;
}
$delay.= " " . _n('day', 'days', $delay, 'storkmdm');

$event->datas['##storkmdm.webapp_url##'] = $config['webapp_url'];
$event->datas['##storkmdm.days_remaining##'] = $delay;

$event->obj->documents = $signatureDocuments;
}
break;

case self::EVENT_POST_TRIAL_REMIND:
if (isset($event->obj)) {
$accountValidation = $event->obj;

$event->obj->documents = $signatureDocuments;
}
}
}

/**
* Return all the targets for this notification
* Values returned by this method are the ones for the alerts
* Can be updated by implementing the getAdditionnalTargets() method
* Can be overwitten (like dbconnection)
* @param $entity the entity on which the event is raised
*/
public function getNotificationTargets($entity) {
$this->addTarget(Notification::USER, __('Registered user', 'storkmdm'));
}

/**
*
* @param array $data
* @param array $options
*/
public function getSpecificTargets($data, $options) {
if ($data['type'] == Notification::USER_TYPE) {
switch ($data['items_id']) {
case Notification::USER:
if ($this->obj->getType() == 'PluginStorkmdmAccountvalidation') {
$this->addToAddressesList([
'users_id' => $this->obj->getField('users_id')
]);
}
break;
}
}
}
}
2 changes: 1 addition & 1 deletion inc/notificationtargetinvitation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getEvents() {
*/
public static function addEvents($target) {
Plugin::loadLang('storkmdm');
$target->events['plugin_storkmdm_invitation'] = __('Invitation', 'storkmdm');
$target->events[self::EVENT_GUEST_INVITATION] = __('Invitation', 'storkmdm');
}

/**
Expand Down
Loading

0 comments on commit 949f7c2

Please sign in to comment.