1 package PVE
::API2
::Group
;
5 use PVE
::Cluster qw
(cfs_read_file cfs_write_file
);
6 use PVE
::AccessControl
;
10 use base
qw(PVE::RESTHandler);
12 __PACKAGE__-
>register_method ({
16 description
=> "Group index.",
18 description
=> "The returned list is restricted to groups where you have 'User.Modify', 'Sys.Audit' or 'Group.Allocate' permissions on /access/groups/<group>.",
22 additionalProperties
=> 0,
30 groupid
=> { type
=> 'string' },
33 links
=> [ { rel
=> 'child', href
=> "{groupid}" } ],
40 my $rpcenv = PVE
::RPCEnvironment
::get
();
41 my $usercfg = cfs_read_file
("user.cfg");
42 my $authuser = $rpcenv->get_user();
44 my $privs = [ 'User.Modify', 'Sys.Audit', 'Group.Allocate'];
46 foreach my $group (keys %{$usercfg->{groups
}}) {
47 next if !$rpcenv->check_any($authuser, "/access/groups/$group", $privs, 1);
48 my $data = $usercfg->{groups
}->{$group};
49 my $entry = { groupid
=> $group };
50 $entry->{comment
} = $data->{comment
} if defined($data->{comment
});
57 __PACKAGE__-
>register_method ({
58 name
=> 'create_group',
63 check
=> ['perm', '/access/groups', ['Group.Allocate']],
65 description
=> "Create new group.",
67 additionalProperties
=> 0,
69 groupid
=> { type
=> 'string', format
=> 'pve-groupid' },
70 comment
=> { type
=> 'string', optional
=> 1 },
73 returns
=> { type
=> 'null' },
77 PVE
::AccessControl
::lock_user_config
(
80 my $usercfg = cfs_read_file
("user.cfg");
82 my $group = $param->{groupid
};
84 die "group '$group' already exists\n"
85 if $usercfg->{groups
}->{$group};
87 $usercfg->{groups
}->{$group} = { users
=> {} };
89 $usercfg->{groups
}->{$group}->{comment
} = $param->{comment
} if $param->{comment
};
92 cfs_write_file
("user.cfg", $usercfg);
93 }, "create group failed");
98 __PACKAGE__-
>register_method ({
99 name
=> 'update_group',
104 check
=> ['perm', '/access/groups', ['Group.Allocate']],
106 description
=> "Update group data.",
108 additionalProperties
=> 0,
110 groupid
=> { type
=> 'string', format
=> 'pve-groupid' },
111 comment
=> { type
=> 'string', optional
=> 1 },
114 returns
=> { type
=> 'null' },
118 PVE
::AccessControl
::lock_user_config
(
121 my $usercfg = cfs_read_file
("user.cfg");
123 my $group = $param->{groupid
};
125 my $data = $usercfg->{groups
}->{$group};
127 die "group '$group' does not exist\n"
130 $data->{comment
} = $param->{comment
} if defined($param->{comment
});
132 cfs_write_file
("user.cfg", $usercfg);
133 }, "update group failed");
138 __PACKAGE__-
>register_method ({
139 name
=> 'read_group',
143 check
=> ['perm', '/access/groups', ['Sys.Audit', 'Group.Allocate'], any
=> 1],
145 description
=> "Get group configuration.",
147 additionalProperties
=> 0,
149 groupid
=> { type
=> 'string', format
=> 'pve-groupid' },
154 additionalProperties
=> 0,
156 comment
=> { type
=> 'string', optional
=> 1 },
168 my $group = $param->{groupid
};
170 my $usercfg = cfs_read_file
("user.cfg");
172 my $data = $usercfg->{groups
}->{$group};
174 die "group '$group' does not exist\n" if !$data;
176 my $members = $data->{users
} ?
[ keys %{$data->{users
}} ] : [];
178 my $res = { members
=> $members };
180 $res->{comment
} = $data->{comment
} if defined($data->{comment
});
186 __PACKAGE__-
>register_method ({
187 name
=> 'delete_group',
192 check
=> ['perm', '/access/groups', ['Group.Allocate']],
194 description
=> "Delete group.",
196 additionalProperties
=> 0,
198 groupid
=> { type
=> 'string' , format
=> 'pve-groupid' },
201 returns
=> { type
=> 'null' },
205 PVE
::AccessControl
::lock_user_config
(
208 my $usercfg = cfs_read_file
("user.cfg");
210 my $group = $param->{groupid
};
212 die "group '$group' does not exist\n"
213 if !$usercfg->{groups
}->{$group};
215 delete ($usercfg->{groups
}->{$group});
217 PVE
::AccessControl
::delete_group_acl
($group, $usercfg);
219 cfs_write_file
("user.cfg", $usercfg);
220 }, "delete group failed");