lib/media_wiki/gateway.rb in mediawiki-gateway-0.3.5 vs lib/media_wiki/gateway.rb in mediawiki-gateway-0.3.6

- old
+ new

@@ -35,11 +35,11 @@ @log.level = @options[:loglevel] @headers = { "User-Agent" => "MediaWiki::Gateway/#{MediaWiki::VERSION}" } @cookies = {} end - attr_reader :base_url + attr_reader :base_url, :cookies # Login to MediaWiki # # [username] Username # [password] Password @@ -457,10 +457,20 @@ params << "format=list" form_data = { 'action' => 'parse', 'prop' => 'text', 'text' => "{{#ask:#{query}|#{params.join('|')}}}" } xml, dummy = make_api_request(form_data) return xml.elements["parse/text"].text end + + # Set groups for a user + # + # [user] Username of user to modify + # [groups_to_add] Groups to add user to, as an array or a string if a single group (optional) + # [groups_to_remove] Groups to remove user from, as an array or a string if a single group (optional) + def set_groups(user, groups_to_add = [], groups_to_remove = [], comment = '') + token = get_userrights_token(user) + userrights(user, token, groups_to_add, groups_to_remove, comment) + end private # Fetch token (type 'delete', 'edit', 'import', 'move') def get_token(type, page_titles) @@ -479,9 +489,45 @@ raise "User is not permitted to perform this operation: #{type}" if token.nil? token else nil end + end + + # User rights management (aka group assignment) + def get_userrights_token(user) + form_data = {'action' => 'query', 'list' => 'users', 'ustoken' => 'userrights', 'ususers' => user} + res, dummy = make_api_request(form_data) + token = res.elements["query/users/user"].attributes["userrightstoken"] + + @log.debug("RESPONSE: #{res.to_s}") + if token.nil? + if res.elements["query/users/user"].attributes["missing"] + raise "User '#{user}' was not found (get_userrights_token)" + else + raise "User '#{@username}' is not permitted to perform this operation: get_userrights_token" + end + end + + token + end + + def userrights(user, token, groups_to_add, groups_to_remove, reason) + # groups_to_add and groups_to_remove can be a string or an array. Turn them into MediaWiki's pipe-delimited list format. + if groups_to_add.is_a? Array + groups_to_add = groups_to_add.join('|') + end + if groups_to_remove.is_a? Array + groups_to_remove = groups_to_remove.join('|') + end + + form_data = {'action' => 'userrights', 'user' => user, 'token' => token, + 'add' => groups_to_add, + 'remove' => groups_to_remove, + 'reason' => reason + } + res, dummy = make_api_request(form_data) + res end # Make generic request to API # # [form_data] hash or string of attributes to post