14
14
use OCP \Settings \IManager ;
15
15
use OCP \Settings \ISettings ;
16
16
use Symfony \Component \Console \Input \InputInterface ;
17
+ use Symfony \Component \Console \Input \InputOption ;
17
18
use Symfony \Component \Console \Output \OutputInterface ;
18
19
use Symfony \Component \Console \Style \SymfonyStyle ;
19
20
@@ -29,39 +30,56 @@ protected function configure(): void {
29
30
$ this
30
31
->setName ('admin-delegation:show ' )
31
32
->setDescription ('show delegated settings ' )
33
+ ->addOption (
34
+ 'output ' ,
35
+ null ,
36
+ InputOption::VALUE_OPTIONAL ,
37
+ 'Output format (plain, json or json_pretty, default is plain) ' ,
38
+ $ this ->defaultOutputFormat
39
+ )
32
40
;
33
41
}
34
42
35
43
protected function execute (InputInterface $ input , OutputInterface $ output ): int {
36
- $ io = new SymfonyStyle ($ input , $ output );
37
- $ io ->title ('Current delegations ' );
38
-
39
44
$ sections = $ this ->settingManager ->getAdminSections ();
45
+
40
46
$ settings = [];
41
- $ headers = ['Name ' , 'SettingId ' , 'Delegated to groups ' ];
42
- foreach ($ sections as $ sectionPriority ) {
43
- foreach ($ sectionPriority as $ section ) {
44
- $ sectionSettings = $ this ->settingManager ->getAdminSettings ($ section ->getId ());
45
- $ sectionSettings = array_reduce ($ sectionSettings , [$ this , 'getDelegatedSettings ' ], []);
46
- if (empty ($ sectionSettings )) {
47
- continue ;
48
- }
47
+ switch ($ input ->getOption ('output ' )) {
48
+ case self ::OUTPUT_FORMAT_JSON :
49
+ $ output ->writeln (json_encode ($ this ->buildJsonOutput ($ sections )));
50
+ break ;
51
+ case self ::OUTPUT_FORMAT_JSON_PRETTY :
52
+ $ output ->writeln (json_encode ($ this ->buildJsonOutput ($ sections ), JSON_PRETTY_PRINT ));
53
+ break ;
54
+ default :
55
+ $ io = new SymfonyStyle ($ input , $ output );
56
+ $ io ->title ('Current delegations ' );
49
57
50
- $ io ->section ('Section: ' . $ section ->getID ());
51
- $ io ->table ($ headers , array_map (function (IDelegatedSettings $ setting ) use ($ section ) {
52
- $ className = get_class ($ setting );
53
- $ groups = array_map (
54
- static fn (AuthorizedGroup $ group ) => $ group ->getGroupId (),
55
- $ this ->authorizedGroupService ->findExistingGroupsForClass ($ className )
56
- );
57
- natsort ($ groups );
58
- return [
59
- $ setting ->getName () ?: 'Global ' ,
60
- $ className ,
61
- implode (', ' , $ groups ),
62
- ];
63
- }, $ sectionSettings ));
64
- }
58
+ $ headers = ['Name ' , 'SettingId ' , 'Delegated to groups ' ];
59
+ foreach ($ sections as $ sectionPriority ) {
60
+ foreach ($ sectionPriority as $ section ) {
61
+ $ sectionSettings = $ this ->settingManager ->getAdminSettings ($ section ->getId ());
62
+ $ sectionSettings = array_reduce ($ sectionSettings , [$ this , 'getDelegatedSettings ' ], []);
63
+ if (empty ($ sectionSettings )) {
64
+ continue ;
65
+ }
66
+
67
+ $ io ->section ('Section: ' . $ section ->getID ());
68
+ $ io ->table ($ headers , array_map (function (IDelegatedSettings $ setting ) use ($ section ) {
69
+ $ className = get_class ($ setting );
70
+ $ groups = array_map (
71
+ static fn (AuthorizedGroup $ group ) => $ group ->getGroupId (),
72
+ $ this ->authorizedGroupService ->findExistingGroupsForClass ($ className )
73
+ );
74
+ natsort ($ groups );
75
+ return [
76
+ $ setting ->getName () ?: 'Global ' ,
77
+ $ className ,
78
+ implode (', ' , $ groups ),
79
+ ];
80
+ }, $ sectionSettings ));
81
+ }
82
+ }
65
83
}
66
84
67
85
return 0 ;
@@ -74,4 +92,40 @@ protected function execute(InputInterface $input, OutputInterface $output): int
74
92
private function getDelegatedSettings (array $ settings , array $ innerSection ): array {
75
93
return $ settings + array_filter ($ innerSection , fn (ISettings $ setting ) => $ setting instanceof IDelegatedSettings);
76
94
}
95
+
96
+ protected function buildJsonOutput (array $ sections ): array {
97
+ $ currentDelegations = [
98
+ 'current_delegations ' => []
99
+ ];
100
+
101
+ foreach ($ sections as $ sectionPriority ) {
102
+ foreach ($ sectionPriority as $ section ) {
103
+ $ sectionSettings = $ this ->settingManager ->getAdminSettings ($ section ->getId ());
104
+ $ sectionSettings = array_reduce ($ sectionSettings , [$ this , 'getDelegatedSettings ' ], []);
105
+ if (empty ($ sectionSettings )) {
106
+ continue ;
107
+ }
108
+
109
+ $ currentDelegations ['current_delegations ' ][] = [
110
+ 'section ' => $ section ->getID (),
111
+ 'delegations ' =>
112
+ array_map (function (IDelegatedSettings $ setting ) use ($ section , $ headers ) {
113
+ $ className = get_class ($ setting );
114
+ $ groups = array_map (
115
+ static fn (AuthorizedGroup $ group ) => $ group ->getGroupId (),
116
+ $ this ->authorizedGroupService ->findExistingGroupsForClass ($ className )
117
+ );
118
+ natsort ($ groups );
119
+ return [
120
+ "name " => $ setting ->getName () ?: 'Global ' ,
121
+ "settingId " => $ className ,
122
+ "delegatedToGroups " => implode (', ' , $ groups )
123
+ ];
124
+ }, $ sectionSettings )
125
+ ];
126
+ }
127
+ }
128
+
129
+ return $ currentDelegations ;
130
+ }
77
131
}
0 commit comments