Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
exportAccounts.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Account export
4  * @author Anakeen
5  * @package FDL
6 */
7 
8 $usage = new ApiUsage();
9 $usage->setDefinitionText("Export accounts definition");
10 /** @noinspection PhpUnusedParameterInspection */
11 $filename = $usage->addRequiredParameter("file", "the output file (use - for stdout)", function ($values, $argName, ApiUsage $apiusage)
12 {
13  if ($values === ApiUsage::GET_USAGE) {
14  return "";
15  }
16  if ($values !== "-" && is_file($values) && !is_writable($values)) {
17  $apiusage->exitError(sprintf("Error: file output \"%s\" not writable.", $values));
18  }
19  return '';
20 });
21 /** @noinspection PhpUnusedParameterInspection */
22 $schemaDirectory = $usage->addOptionalParameter("schema-directory", "directory where produce xsd for documents", function ($values, $argName, ApiUsage $apiusage)
23 {
24  if ($values === ApiUsage::GET_USAGE) {
25  return "";
26  }
27  if ($values !== "-" && is_dir($values) && !is_writable($values)) {
28  $apiusage->exitError(sprintf("Error: directory output \"%s\" not writable.", $values));
29  }
30  return '';
31 });
32 /**
33  * @var \Account $memberAccount
34  */
36 /** @noinspection PhpUnusedParameterInspection */
37 $memberOf = $usage->addOptionalParameter("memberOf", "Restrict to account which are member of this group or role reference", function ($values, $argName, ApiUsage $apiusage) use (&$memberAccount)
38 {
39  if ($values) {
40  if ($values === ApiUsage::GET_USAGE) {
41  return "";
42  }
43  $memberAccount = new Account();
44  if (!$memberAccount->setLoginName($values)) {
45  $apiusage->exitError(sprintf("Error: member reference \"%s\" unknow.", $values));
46  }
47  }
48  return '';
49 });
50 
51 $exportPassword = $usage->addEmptyParameter("crypt-password", "add crypt password");
52 $exportRole = $usage->addEmptyParameter("roles", "export associated roles");
53 $exportGroup = $usage->addEmptyParameter("groups", "export parent groups");
54 $exportDocument = $usage->addEmptyParameter("document", "export specific document information");
55 /** @noinspection PhpUnusedParameterInspection */
56 $type = $usage->addOptionalParameter("type", "restricted to account type", function ($values, $argName, ApiUsage $apiusage)
57 {
58  $opt = array(
59  "user",
60  "role",
61  "group"
62  );
63  if ($values === ApiUsage::GET_USAGE) return sprintf(" [%s] ", implode('|', $opt));
64 
65  $error = $apiusage->matchValues($values, $opt);
66  if ($error) {
67  $apiusage->exitError(sprintf("Error: wrong value for argument 'type' : %s", $error));
68  }
69  return '';
70 });
71 $filterLogin = $usage->addOptionalParameter("login-filter", "filter login contains");
72 $usage->verify();
73 
74 $export = new \Dcp\Core\ExportAccounts();
76 if ($filterLogin) {
77  $search->addFilter(sprintf("login ~* '%s'", $filterLogin));
78 }
79 if ($memberOf && $memberAccount) {
80  if ($memberAccount->accounttype === Account::GROUP_TYPE) {
81  $search->addGroupFilter($memberAccount->login);
82  }
83  if ($memberAccount->accounttype === Account::ROLE_TYPE) {
84  $search->addRoleFilter($memberAccount->login);
85  }
86 }
87 
88 if ($type) {
89  if (!is_array($type)) {
90  $type = array(
91  $type
92  );
93  }
94  $accountType = 0;
95  foreach ($type as $singleType) {
96  switch ($singleType) {
97  case "user":
98  $accountType|= \SearchAccount::userType;
99  break;
100 
101  case "group":
102  $accountType|= \SearchAccount::groupType;
103  break;
104 
105  case "role":
106  $accountType|= \SearchAccount::roleType;
107  break;
108  }
109  }
110  $search->setTypeFilter($accountType);
111 }
112 if ($schemaDirectory) {
113  $export->setExportSchemaDirectory($schemaDirectory);
114 }
115 $export->setExportGroupParent($exportGroup);
116 $export->setExportRoleParent($exportRole);
117 $export->setExportCryptedPassword($exportPassword);
118 $export->setExportDocument($exportDocument);
119 $export->setSearchAccount($search);
120 
121 if ($filename === "-") {
122  $filename = "php://stdout";
123 }
124 file_put_contents($filename, $export->export());
$filterLogin
$memberOf
exitError($error= '')
$filename
$exportPassword
const GROUP_TYPE
const GET_USAGE
$search
$exportRole
$exportGroup
$usage
$exportDocument
static matchValues($value, $restrictions)
$schemaDirectory
$export
Verify arguments for wsh programs.
$memberAccount
const ROLE_TYPE
← centre documentaire © anakeen