Permalink
Browse files

[api] cleanup group and user handling, showing groups as xml and hand…

…le user lookup errors via exceptions
  • Loading branch information...
1 parent 0eba277 commit 6bf9c8eda4961ac3bc402e3240a76ad977b635ae @adrianschroeter adrianschroeter committed Mar 31, 2011
View
@@ -73,7 +73,15 @@ Changes:
* api
- It was not possible so far to create submit requests from packages where no write access exists.
This is possible now, but the source package maintainer will get asked for review the request.
+ - the route /group/$GROUP is showing correct xml description and no directory anymore
+Deprecated:
+===========
+
+The following calls have been marked as deprecated, they will get removed in OBS 3.0
+
+* api
+ - /person/$LOGIN/group -> use /group?login=$LOGIN instead
Requirements:
=============
@@ -12,6 +12,7 @@ class MissingParameterError < Exception; end
class IllegalRequestError < Exception; end
class IllegalEncodingError < Exception; end
class UserNotFoundError < Exception; end
+class GroupNotFoundError < Exception; end
class TagNotFoundError < Exception; end
class ApplicationController < ActionController::Base
@@ -473,6 +474,15 @@ def rescue_action_in_public( exception )
render_error :status => 404, :errorcode => 'user_not_found',
:message => exception.message
end
+ when GroupNotFoundError
+ logger.error "GroupNotFoundError: #{exception.message}"
+ if exception.message == ""
+ render_error :status => 404, :errorcode => 'group_not_found',
+ :message => "Group not found"
+ else
+ render_error :status => 404, :errorcode => 'group_not_found',
+ :message => exception.message
+ end
else
if send_exception_mail?
ExceptionNotifier.deliver_exception_notification(exception, self, request, {})
@@ -7,11 +7,15 @@ class GroupController < ApplicationController
def index
valid_http_methods :get
- if params[:prefix]
- list = Group.find(:all, :conditions => ["title LIKE ?", params[:prefix] + '%'])
+ if params[:login]
+ user = User.get_by_login(params[:login])
+ list = user.groups
else
list = Group.find(:all)
end
+ if params[:prefix]
+ list = list.find_all{|l| l.match(/^#{params[:prefix]}/)}
+ end
builder = Builder::XmlMarkup.new(:indent => 2)
xml = builder.directory(:count => list.length) do |dir|
@@ -20,6 +24,7 @@ def index
render :text => xml, :content_type => "text/xml"
end
+ # OBSOLETE with 3.0
def grouplist
valid_http_methods :get
@@ -49,6 +54,14 @@ def grouplist
render :text => xml, :content_type => "text/xml" and return
end
+ def show
+ valid_http_methods :get
+ required_parameters :title
+
+ @group = Group.get_by_title( params[:title] )
+ @involved_users = GroupsUser.find(:all, :conditions => ["group_id = ?", @group])
+ end
+
private
# filter to check if a user is logged in
@@ -49,13 +49,7 @@ def userinfo
if params[:login]
login = URI.unescape( params[:login] )
logger.debug "Generating for user from parameter #{login}"
- @render_user = User.find_by_login( login )
- if @render_user.blank?
- logger.debug "User is not valid!"
- render_error :status => 404, :errorcode => 'unknown_user',
- :message => "Unknown user: #{login}"
- return
- end
+ @render_user = User.get_by_login( login )
else
logger.debug "Generating user info for logged in user #{@http_user.login}"
@render_user = @http_user
@@ -280,13 +274,7 @@ def change_my_password
end
#update password in users db
- @user = User.find_by_login(login)
- if @user.blank?
- logger.debug "User is not valid!"
- render_error :status => 404, :errorcode => 'unknown_user',
- :message => "Unknown user: #{login}"
- return
- end
+ @user = User.get_by_login(login)
logger.debug("find the user")
@user.password = newpassword
@user.password_confirmation = newpassword
@@ -52,6 +52,12 @@ def render_group_list(user=nil)
return xml
end
+
+ def get_by_title(title)
+ g = find :first, :conditions => ["title = BINARY ?", title]
+ raise GroupNotFoundError.new( "Error: Group '#{title}' not found." ) unless g
+ return g
+ end
end
def render_axml
View
@@ -15,28 +15,36 @@ class User < ActiveRecord::Base
has_many :status_messages
has_many :messages
- def self.current
- Thread.current[:user]
- end
-
- def self.currentID
- Thread.current[:id]
- end
-
- def self.currentAdmin
- Thread.current[:admin]
- end
+ class << self
+ def current
+ Thread.current[:user]
+ end
+
+ def currentID
+ Thread.current[:id]
+ end
+
+ def currentAdmin
+ Thread.current[:admin]
+ end
- def self.current=(user)
- Thread.current[:user] = user
- end
+ def current=(user)
+ Thread.current[:user] = user
+ end
- def self.currentID=(id)
- Thread.current[:id] = id
- end
+ def currentID=(id)
+ Thread.current[:id] = id
+ end
- def self.currentAdmin=(isadmin)
- Thread.current[:admin] = isadmin
+ def currentAdmin=(isadmin)
+ Thread.current[:admin] = isadmin
+ end
+
+ def get_by_login(login)
+ u = find :first, :conditions => ["login = BINARY ?", login]
+ raise UserNotFoundError.new( "Error: User '#{login}' not found." ) unless u
+ return u
+ end
end
def encrypt_password
@@ -0,0 +1,10 @@
+xml.group do
+ xml.title @group.title
+
+ xml.person do
+ @involved_users.each do |gu|
+ xml.person :userid => gu.user.login
+ end
+ end
+
+end
View
@@ -24,19 +24,16 @@
# fix this for OBS 3.0
map.connect 'person/register', :controller => 'person', :action => 'register'
map.connect 'person/changepasswd', :controller => 'person', :action => 'change_my_password'
- map.connect 'person/:login', :controller => 'person', :action => 'userinfo', :login => /[^\/]*/
+ # bad api, to be removed for OBS 3. Use /group?person=:login instead
map.connect 'person/:login/group', :controller => 'person', :action => 'grouplist', :login => /[^\/]*/
+ map.connect 'person/:login', :controller => 'person', :action => 'userinfo', :login => /[^\/]*/
+
### /group
map.connect 'group', :controller => 'group', :action => 'index'
- map.connect 'group/:title', :controller => 'group', :action => 'grouplist', :title => /[^\/]*/
-
- ### /repository
-
- map.connect 'repository', :controller => 'repository', :action => 'index'
+ map.connect 'group/:title', :controller => 'group', :action => 'show', :title => /[^\/]*/
### /service
-
map.connect 'service/:service', :controller => "service",
:action => 'index_service', :service => /\w[^\/]*/
@@ -29,7 +29,7 @@ def test_namespace_index
get "/attribute/OBS"
assert_response :success
- count = 7
+ count = 8
assert_tag :tag => 'directory', :attributes => { :count => count }
assert_tag :children => { :count => count }
assert_tag :child => { :tag => 'entry', :attributes => { :name => "Maintained" } }
@@ -160,7 +160,7 @@ def test_attrib_type_meta
get "/attribute/OBS"
assert_response :success
- count = 7
+ count = 8
assert_tag :tag => 'directory', :attributes => { :count => count }
assert_tag :children => { :count => count }
assert_tag :child => { :tag => 'entry', :attributes => { :name => "Maintained" } }
@@ -14,6 +14,7 @@ def test_list_groups
assert_response :success
assert_tag :tag => 'directory', :child => {:tag => 'entry'}
assert_tag :tag => 'entry', :attributes => {:name => 'test_group'}
+ assert_tag :tag => 'entry', :attributes => {:name => 'test_group_b'}
end
def test_list_users_of_group
@@ -26,8 +27,8 @@ def test_list_users_of_group
assert_response 404
get "/group/test_group"
assert_response :success
- assert_tag :tag => 'directory', :child => {:tag => 'entry'}
- assert_tag :tag => 'entry', :attributes => {:name => 'adrian'}
+ assert_tag :tag => 'group', :child => {:tag => 'title'}, :content => "test_group"
+ assert_tag :tag => 'person', :attributes => {:userid => 'adrian'}
end
def test_groups_of_user
@@ -36,10 +37,19 @@ def test_groups_of_user
assert_response 401
prepare_request_valid_user
+ # old way, obsolete with OBS 3
get "/person/adrian/group"
assert_response :success
assert_tag :tag => 'directory', :child => {:tag => 'entry'}
assert_tag :tag => 'entry', :attributes => {:name => 'test_group'}
+ assert_no_tag :tag => 'entry', :attributes => {:name => 'test_group_b'}
+
+ # new way, standard since OBS 2.3
+ get "/group?login=adrian"
+ assert_response :success
+ assert_tag :tag => 'directory', :child => {:tag => 'entry'}
+ assert_tag :tag => 'entry', :attributes => {:name => 'test_group'}
+ assert_no_tag :tag => 'entry', :attributes => {:name => 'test_group_b'}
end
end

0 comments on commit 6bf9c8e

Please sign in to comment.