<?php 
require_once __DIR__.SLASH.'ActiveDirectoryGroup.class.php';
global $_,$conf,$myUser;
switch($_['action']){
	case 'activedirectory_activedirectory_settings_save':
		if($myUser->can('activedirectory','configure')){
			$conf->put('plugin_activedirectory_server',$_['ad-server']);
			$conf->put('plugin_activedirectory_port',$_['ad-port']);
			$conf->put('plugin_activedirectory_ssl_port',$_['ad-ssl-port']);
			$conf->put('plugin_activedirectory_user_root',$_['ad-user-root']);
			$conf->put('plugin_activedirectory_group_root',$_['ad-group-root']);
			$conf->put('plugin_activedirectory_domain',$_['ad-domain']);

			$conf->put('plugin_activedirectory_reader_login',$_['ad-reader-login']);
			$conf->put('plugin_activedirectory_reader_password',$_['ad-reader-password']);
			
			$conf->put('plugin_activedirectory_admin_login',$_['ad-admin-login']);
			$conf->put('plugin_activedirectory_admin_password',$_['ad-admin-password']);

		}
		header('location: setting.php?section=activedirectory&success=Configuration enregistrée');
	break;

	case 'activedirectory_activedirectory_save_link':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('activedirectory','edit')) throw new Exception("Permissions insuffisantes",403);
			if(!isset($_['ad-group']) || empty($_['ad-group'])) throw new Exception("Nom de groupe obligatoire");

			$item = ActiveDirectoryGroup::provide();
			$item->adgroup = $_['ad-group'];
			$item->rank = $_['ad-rank'];
			$item->firm = $_['ad-firm'];
			$item->save();

		});
	break;

	case 'activedirectory_activedirectory_edit_link':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('activedirectory','edit')) throw new Exception("Permissions insuffisantes",403);
			$adgroup = ActiveDirectoryGroup::getById($_['id']);
			$adgroup = $adgroup->toArray();
			$adgroup['ad-group'] = html_entity_decode($adgroup['adgroup']);
			$adgroup['ad-rank'] = html_entity_decode($adgroup['rank']);
			$adgroup['ad-firm'] = html_entity_decode($adgroup['firm']);
			$response = $adgroup;
		});
	break;

	case 'activedirectory_activedirectory_delete_link':
		global $_;
		if(!$myUser->can('activedirectory','delete')) throw new Exception("Permissions insuffisantes",403);
		ActiveDirectoryGroup::deleteById($_['groupId']);

		header('location: setting.php?section=activedirectory&info=Correspondance Groupe/Rang/Établissement supprimée');
	break;
}