java/code/src/com/redhat/rhn/frontend/xmlrpc/channel/software/ChannelSoftwareHandler.java
| 82 ++++++++++
java/code/src/com/redhat/rhn/manager/channel/ChannelManager.java
| 70 ++++++++
2 files changed, 152 insertions(+)
New commits:
commit 2f15fa6f0d6d1be52fb7f3acfe024480c2c5ac62
Author: Tomas Lestach <tlestach(a)redhat.com>
Date: Thu Jun 23 17:46:54 2011 +0200
699505 - introduce channel.software.setUserManagable and
channel.software.isUserManagable API calls
diff --git
a/java/code/src/com/redhat/rhn/frontend/xmlrpc/channel/software/ChannelSoftwareHandler.java
b/java/code/src/com/redhat/rhn/frontend/xmlrpc/channel/software/ChannelSoftwareHandler.java
index 9e4f2d5..35a3ba9 100644
---
a/java/code/src/com/redhat/rhn/frontend/xmlrpc/channel/software/ChannelSoftwareHandler.java
+++
b/java/code/src/com/redhat/rhn/frontend/xmlrpc/channel/software/ChannelSoftwareHandler.java
@@ -1013,6 +1013,54 @@ public class ChannelSoftwareHandler extends BaseHandler {
}
/**
+ * Set the managable flag for a given channel and user. If value is set to
'true',
+ * this method will give the user manage permissions to the channel. Otherwise, this
+ * method revokes that privilege.
+ * @param sessionKey The sessionKey containing the logged in user
+ * @param channelLabel The label for the channel in question
+ * @param login The login for the user in question
+ * @param value The boolean value telling us whether to grant manage permission or
+ * revoke it.
+ * @return Returns 1 on success, FaultException otherwise
+ * @throws FaultException A FaultException is thrown if:
+ * - The loggedInUser doesn't have permission to perform this action
+ * - The login, sessionKey, or channelLabel is invalid
+ *
+ * @xmlrpc.doc Set the managable flag for a given channel and user.
+ * If value is set to 'true', this method will give the user
+ * manage permissions to the channel. Otherwise, that privilege is revoked.
+ * @xmlrpc.param #session_key()
+ * @xmlrpc.param #param_desc("string", "channelLabel",
"label of the channel")
+ * @xmlrpc.param #param_desc("string", "login", "login of
the target user")
+ * @xmlrpc.param #param_desc("boolean", "value", "value of
the flag to set")
+ * @xmlrpc.returntype #return_int_success()
+ */
+ public int setUserManagable(String sessionKey, String channelLabel,
+ String login, Boolean value) throws FaultException {
+ // Get Logged in user
+ User loggedInUser = getLoggedInUser(sessionKey);
+ User target = XmlRpcUserHelper.getInstance().lookupTargetUser(loggedInUser,
login);
+
+ Channel channel = lookupChannelByLabel(loggedInUser, channelLabel);
+ //Verify permissions
+ if (!(UserManager.verifyChannelAdmin(loggedInUser, channel) ||
+ loggedInUser.hasRole(RoleFactory.CHANNEL_ADMIN))) {
+ throw new PermissionCheckFailureException();
+ }
+
+ if (value) {
+ // Add the 'manage' role for the target user to the channel
+ ChannelManager.addManageRole(target, channel);
+ }
+ else {
+ // Remove the 'manage' role for the target user to the channel
+ ChannelManager.removeManageRole(target, channel);
+ }
+
+ return 1;
+ }
+
+ /**
* Returns whether the channel may be subscribed to by the given user.
* @param sessionKey The sessionKey containing the logged in user
* @param channelLabel The label for the channel in question
@@ -1047,6 +1095,40 @@ public class ChannelSoftwareHandler extends BaseHandler {
}
/**
+ * Returns whether the channel may be managed by the given user.
+ * @param sessionKey The sessionKey containing the logged in user
+ * @param channelLabel The label for the channel in question
+ * @param login The login for the user in question
+ * @return whether the channel may be managed by the given user.
+ * @throws FaultException thrown if
+ * - The loggedInUser doesn't have permission to perform this action
+ * - The login, sessionKey, or channelLabel is invalid
+ *
+ * @xmlrpc.doc Returns whether the channel may be managed by the given user.
+ * @xmlrpc.param #session_key()
+ * @xmlrpc.param #param_desc("string", "channelLabel",
"label of the channel")
+ * @xmlrpc.param #param_desc("string", "login", "login of
the target user")
+ * @xmlrpc.returntype int - 1 if subscribable, 0 if not
+ */
+ public int isUserManagable(String sessionKey, String channelLabel,
+ String login) throws FaultException {
+ // Get Logged in user
+ User loggedInUser = getLoggedInUser(sessionKey);
+ User target = XmlRpcUserHelper.getInstance().lookupTargetUser(
+ loggedInUser, login);
+
+ Channel channel = lookupChannelByLabel(loggedInUser.getOrg(), channelLabel);
+ //Verify permissions
+ if (!(UserManager.verifyChannelAdmin(loggedInUser, channel) ||
+ loggedInUser.hasRole(RoleFactory.CHANNEL_ADMIN))) {
+ throw new PermissionCheckFailureException();
+ }
+
+ boolean flag = ChannelManager.verifyChannelManage(target, channel.getId());
+ return BooleanUtils.toInteger(flag);
+ }
+
+ /**
* Set globally subscribable attribute for given channel.
* @param sessionKey The sessionKey containing the logged in user
* @param channelLabel The label for the channel to change
diff --git a/java/code/src/com/redhat/rhn/manager/channel/ChannelManager.java
b/java/code/src/com/redhat/rhn/manager/channel/ChannelManager.java
index f650fbc..9e045b9 100644
--- a/java/code/src/com/redhat/rhn/manager/channel/ChannelManager.java
+++ b/java/code/src/com/redhat/rhn/manager/channel/ChannelManager.java
@@ -982,6 +982,44 @@ public class ChannelManager extends BaseManager {
}
/**
+ * Adds the mange role for the passed in user for the passed in channel.
+ * @param user The user in question.
+ * @param channel The channel in question.
+ */
+ public static void addManageRole(User user, Channel channel) {
+ if (verifyChannelManage(user, channel.getId())) {
+ return; //user already has subscribe perms to this channel
+ }
+ //Insert row into rhnChannelPermission
+ WriteMode m = ModeFactory.getWriteMode("Channel_queries",
+ "grant_channel_permission");
+ Map params = new HashMap();
+ params.put("user_id", user.getId());
+ params.put("cid", channel.getId());
+ params.put("role_label", QRY_ROLE_MANAGE);
+ m.executeUpdate(params);
+ }
+
+ /**
+ * Removes the manage role from the passed in user for the passed in channel.
+ * @param user The user in question.
+ * @param channel The channel in question.
+ */
+ public static void removeManageRole(User user, Channel channel) {
+ if (!verifyChannelManage(user, channel.getId())) {
+ return; //user doesn't have subscribe perms to begin with
+ }
+ //Delete row from rhnChannelPermission
+ WriteMode m = ModeFactory.getWriteMode("Channel_queries",
+ "revoke_channel_permission");
+ Map params = new HashMap();
+ params.put("user_id", user.getId());
+ params.put("cid", channel.getId());
+ params.put("role_label", QRY_ROLE_MANAGE);
+ m.executeUpdate(params);
+ }
+
+ /**
* Makes sure the passed in user has subscribe permissions to the channel with the
* given id.
* @param user The user in question
@@ -1014,6 +1052,38 @@ public class ChannelManager extends BaseManager {
}
/**
+ * Makes sure the passed in user has manage permissions to the channel with the
+ * given id.
+ * @param user The user in question
+ * @param cid The id for the channel in question
+ * @return Returns true if the user has permission, false otherwise
+ */
+ public static boolean verifyChannelManage(User user, Long cid) {
+
+ if (user.hasRole(RoleFactory.RHN_SUPERUSER)) {
+ return true;
+ }
+
+ try {
+ return verifyChannelRole(user, cid, QRY_ROLE_MANAGE);
+ }
+ catch (InvalidChannelRoleException e) {
+ /*
+ * We don't really care what the reason is for why this user doesn't
have
+ * access to this channel, so catch the exception, log it, and simply
+ * return false.
+ */
+ StringBuffer msg = new StringBuffer("User: ");
+ msg.append(user.getLogin());
+ msg.append(" either does not have subscribe privileges to Channel:
");
+ msg.append(cid);
+ msg.append(" or ChannelManager.QRY_ROLE_MANAGE is defined
wrong.");
+ log.debug(msg.toString(), e);
+ return false;
+ }
+ }
+
+ /**
* Check to see if the channel passed in is subscribable by the Server passed in for
* *free* without costing an entitlement. Criteria:
*