2007-03-30 04:36:52 +00:00
|
|
|
class BackendController < ApplicationController
|
|
|
|
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
|
|
|
|
|
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)
|
2007-03-30 04:36:52 +00:00
|
|
|
description,context = split_by_char('@',description)
|
|
|
|
description,project = split_by_char('>',description)
|
|
|
|
if(!context.nil? && project.nil?)
|
|
|
|
context,project = split_by_char('>',context)
|
|
|
|
end
|
2008-05-16 19:38:45 +00:00
|
|
|
# logger.info("context='#{context}' project='#{project}")
|
2007-03-30 04:36:52 +00:00
|
|
|
|
|
|
|
context_id = default_context_id
|
|
|
|
unless(context.nil?)
|
2008-05-16 19:38:45 +00:00
|
|
|
found_context = @user.active_contexts.find_by_namepart(context)
|
|
|
|
found_context = @user.contexts.find_by_namepart(context) if found_context.nil?
|
2007-03-30 04:36:52 +00:00
|
|
|
context_id = found_context.id unless found_context.nil?
|
|
|
|
end
|
|
|
|
check_context_belongs_to_user(context_id)
|
|
|
|
|
|
|
|
project_id = nil
|
|
|
|
unless(project.blank?)
|
|
|
|
if(project[0..3].downcase == "new:")
|
|
|
|
found_project = @user.projects.build
|
|
|
|
found_project.name = project[4..255+4].strip
|
|
|
|
found_project.save!
|
|
|
|
else
|
2008-05-16 19:38:45 +00:00
|
|
|
found_project = @user.active_projects.find_by_namepart(project)
|
|
|
|
found_project = @user.projects.find_by_namepart(project) if found_project.nil?
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
project_id = found_project.id unless found_project.nil?
|
|
|
|
end
|
|
|
|
|
2007-09-26 11:40:14 +00:00
|
|
|
todo = create_todo(description, context_id, project_id, notes)
|
2007-03-30 04:36:52 +00:00
|
|
|
todo.id
|
|
|
|
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
|
|
|
|
2008-01-25 20:39:44 +00:00
|
|
|
def split_by_char(separator,string)
|
|
|
|
parts = string.split(separator)
|
|
|
|
|
|
|
|
# if the separator is used more than once, concat the last parts this is
|
|
|
|
# needed to get 'description @ @home > project' working for contexts
|
|
|
|
# starting with @
|
|
|
|
if parts.length > 2
|
2008-05-16 19:38:45 +00:00
|
|
|
2.upto(parts.length-1) { |i| parts[1] += (separator +parts[i]) }
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2008-01-25 20:39:44 +00:00
|
|
|
|
|
|
|
return safe_strip(parts[0]), safe_strip(parts[1])
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
|
2008-01-25 20:39:44 +00:00
|
|
|
def safe_strip(s)
|
|
|
|
s.strip! unless s.nil?
|
|
|
|
s
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class InvalidToken < RuntimeError; end
|
|
|
|
class CannotAccessContext < RuntimeError; end
|