1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

Feature #1742: Methods to add and remove secondary groups

This commit is contained in:
Carlos Martín 2013-08-23 13:30:06 +02:00
parent 7bfb930292
commit 1b3a10b958
5 changed files with 206 additions and 2 deletions

View File

@ -128,6 +128,48 @@ public:
string& err);
};
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class UserAddGroup : public RequestManagerUser
{
public:
UserAddGroup():
RequestManagerUser("UserAddGroup",
"Adds the user to a secondary group",
"A:sii")
{
auth_op = AuthRequest::MANAGE;
};
~UserAddGroup(){};
int user_action(int user_id,
xmlrpc_c::paramList const& _paramList,
string& err);
};
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class UserDelGroup : public RequestManagerUser
{
public:
UserDelGroup():
RequestManagerUser("UserDelGroup",
"Deletes the user from a secondary group",
"A:sii")
{
auth_op = AuthRequest::MANAGE;
};
~UserDelGroup(){};
int user_action(int user_id,
xmlrpc_c::paramList const& _paramList,
string& err);
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -380,7 +380,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
chgrp_desc = <<-EOT.unindent
Changes the User's main group
Changes the User's primary group
EOT
command :chgrp, chgrp_desc, [:range, :userid_list], :groupid do
@ -389,6 +389,30 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
end
addgroup_desc = <<-EOT.unindent
Adds the User to a secondary group
EOT
command :addgroup, addgroup_desc, [:range, :userid_list], :groupid do
gid = args[1]
helper.perform_actions(args[0],options,"group added") do |user|
user.addgroup( gid )
end
end
delgroup_desc = <<-EOT.unindent
Removes the User from a secondary group
EOT
command :delgroup, delgroup_desc, [:range, :userid_list], :groupid do
gid = args[1]
helper.perform_actions(args[0],options,"group deleted") do |user|
user.delgroup( gid )
end
end
chauth_desc = <<-EOT.unindent
Changes the User's auth driver and its password (optional)
Examples:

View File

@ -29,6 +29,8 @@ module OpenNebula
:delete => "user.delete",
:passwd => "user.passwd",
:chgrp => "user.chgrp",
:addgroup => "user.addgroup",
:delgroup => "user.delgroup",
:update => "user.update",
:chauth => "user.chauth",
:quota => "user.quota"
@ -124,7 +126,7 @@ module OpenNebula
return rc
end
# Changes the main group
# Changes the primary group
# gid:: _Integer_ the new group id. Set to -1 to leave the current one
# [return] nil in case of success or an Error object
def chgrp(gid)
@ -136,6 +138,23 @@ module OpenNebula
return rc
end
# Adds the User to a secondary group
# @param gid [Integer] the new group id.
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def addgroup(gid)
return call(USER_METHODS[:addgroup], @pe_id, gid)
end
# Removes the User from a secondary group. Fails if the
# group is the main one
# @param gid [Integer] the group id.
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def delgroup(gid)
return call(USER_METHODS[:delgroup], @pe_id, gid)
end
# Changes the auth driver and the password of the given User
#
# @param auth [String] the new auth driver

View File

@ -254,6 +254,8 @@ void RequestManager::register_xml_methods()
xmlrpc_c::methodPtr user_change_password(new UserChangePassword());
xmlrpc_c::methodPtr user_change_auth(new UserChangeAuth());
xmlrpc_c::methodPtr user_set_quota(new UserSetQuota());
xmlrpc_c::methodPtr user_add_group(new UserAddGroup());
xmlrpc_c::methodPtr user_del_group(new UserDelGroup());
// Group Methods
xmlrpc_c::methodPtr group_set_quota(new GroupSetQuota());
@ -489,6 +491,8 @@ void RequestManager::register_xml_methods()
RequestManagerRegistry.addMethod("one.user.info", user_info);
RequestManagerRegistry.addMethod("one.user.passwd", user_change_password);
RequestManagerRegistry.addMethod("one.user.chgrp", user_chown);
RequestManagerRegistry.addMethod("one.user.addgroup", user_add_group);
RequestManagerRegistry.addMethod("one.user.delgroup", user_del_group);
RequestManagerRegistry.addMethod("one.user.chauth", user_change_auth);
RequestManagerRegistry.addMethod("one.user.quota", user_set_quota);

View File

@ -210,3 +210,118 @@ int UserSetQuota::user_action(int user_id,
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int UserAddGroup::user_action(
int user_id,
xmlrpc_c::paramList const& paramList,
string& error_str)
{
int group_id = xmlrpc_c::value_int(paramList.getInt(2));
int rc;
User* user = static_cast<User *>(pool->get(user_id,true));
rc = user->add_group(group_id);
if ( rc != 0 )
{
user->unlock();
error_str = "User is already in this group";
return rc;
}
pool->update(user);
user->unlock();
Nebula& nd = Nebula::instance();
GroupPool * gpool = nd.get_gpool();
Group * group = gpool->get(group_id, true);
if( group == 0 )
{
User * user = static_cast<User *>(pool->get(user_id,true));
if ( user != 0 )
{
user->del_group(group_id);
pool->update(user);
user->unlock();
}
error_str = "Group does not exist";
return -1;
}
group->add_user(user_id);
gpool->update(group);
group->unlock();
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int UserDelGroup::user_action(
int user_id,
xmlrpc_c::paramList const& paramList,
string& error_str)
{
int group_id = xmlrpc_c::value_int(paramList.getInt(2));
int rc;
User* user = static_cast<User *>(pool->get(user_id,true));
rc = user->del_group(group_id);
if ( rc != 0 )
{
user->unlock();
if ( rc == -1 )
{
error_str = "User is not part of this group";
}
else if ( rc == -2 )
{
error_str = "Cannot remove user from the primary group";
}
else
{
error_str = "Cannot remove user from group";
}
return rc;
}
pool->update(user);
user->unlock();
Nebula& nd = Nebula::instance();
GroupPool * gpool = nd.get_gpool();
Group * group = gpool->get(group_id, true);
if( group == 0 )
{
//Group does not exists, should never occur
error_str = "Cannot remove user from group";
return -1;
}
group->del_user(user_id);
gpool->update(group);
group->unlock();
return 0;
}