2011-02-26 14:20:51 +01:00
|
|
|
class CannotAccessContext < RuntimeError; end
|
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
class BackendController < ApplicationController
|
2011-10-09 20:30:13 +02:00
|
|
|
acts_as_web_service
|
2007-03-30 04:36:52 +00:00
|
|
|
wsdl_service_name 'Backend'
|
|
|
|
|
web_service_api TodoApi
|
|
|
|
|
web_service_scaffold :invoke
|
|
|
|
|
skip_before_filter :login_required
|
|
|
|
|
|
|
|
|
|
|
2007-09-26 11:40:14 +00:00
|
|
|
def new_todo(username, token, context_id, description, notes)
|
2007-07-17 03:50:13 +00:00
|
|
|
check_token(username, token)
|
2007-03-30 04:36:52 +00:00
|
|
|
check_context_belongs_to_user(context_id)
|
2007-09-26 11:40:14 +00:00
|
|
|
item = create_todo(description, context_id, nil, notes)
|
2007-03-30 04:36:52 +00:00
|
|
|
item.id
|
|
|
|
|
end
|
2009-03-12 13:05:55 +08:00
|
|
|
|
|
|
|
|
def new_todo_for_project(username, token, context_id, project_id, description, notes)
|
|
|
|
|
check_token(username, token)
|
|
|
|
|
check_context_belongs_to_user(context_id)
|
|
|
|
|
item = create_todo(description, context_id, project_id, notes)
|
|
|
|
|
item.id
|
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
|
2007-09-26 11:40:14 +00:00
|
|
|
def new_rich_todo(username, token, default_context_id, description, notes)
|
2007-07-17 03:50:13 +00:00
|
|
|
check_token(username,token)
|
2008-09-21 09:35:34 -07:00
|
|
|
item = Todo.from_rich_message(@user, default_context_id, description, notes)
|
|
|
|
|
item.save
|
|
|
|
|
raise item.errors.full_messages.to_s if item.new_record?
|
|
|
|
|
item.id
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def list_contexts(username, token)
|
2007-07-17 03:50:13 +00:00
|
|
|
check_token(username, token)
|
2007-03-30 04:36:52 +00:00
|
|
|
|
|
|
|
|
@user.contexts
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def list_projects(username, token)
|
2007-07-17 03:50:13 +00:00
|
|
|
check_token(username, token)
|
2007-03-30 04:36:52 +00:00
|
|
|
|
|
|
|
|
@user.projects
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2008-01-25 20:39:44 +00:00
|
|
|
# Check whether the token in the URL matches the token in the User's table
|
|
|
|
|
def check_token(username, token)
|
|
|
|
|
@user = User.find_by_login( username )
|
|
|
|
|
unless (token == @user.token)
|
|
|
|
|
raise(InvalidToken, "Sorry, you don't have permission to perform this action.")
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2008-01-25 20:39:44 +00:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
|
2008-01-25 20:39:44 +00:00
|
|
|
def check_context_belongs_to_user(context_id)
|
|
|
|
|
unless @user.contexts.exists? context_id
|
|
|
|
|
raise(CannotAccessContext, "Cannot access a context that does not belong to this user.")
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2008-01-25 20:39:44 +00:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
|
2008-01-25 20:39:44 +00:00
|
|
|
def create_todo(description, context_id, project_id = nil, notes="")
|
|
|
|
|
item = @user.todos.build
|
|
|
|
|
item.description = description
|
|
|
|
|
item.notes = notes
|
|
|
|
|
item.context_id = context_id
|
|
|
|
|
item.project_id = project_id unless project_id.nil?
|
|
|
|
|
item.save
|
|
|
|
|
raise item.errors.full_messages.to_s if item.new_record?
|
|
|
|
|
item
|
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class InvalidToken < RuntimeError; end
|