Added postgresql support.

This commit is contained in:
Martin Edenhofer 2016-01-20 02:48:54 +01:00
parent a2b68280b9
commit c775463fd8
3 changed files with 44 additions and 46 deletions

View file

@ -16,13 +16,13 @@ class UsersController < ApplicationController
# only allow customer to fetch him self # only allow customer to fetch him self
users = if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?('Agent') users = if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?('Agent')
User.where( id: current_user.id ) User.where(id: current_user.id)
else else
User.all User.all
end end
users_all = [] users_all = []
users.each {|user| users.each {|user|
users_all.push User.lookup( id: user.id ).attributes_with_associations users_all.push User.lookup(id: user.id).attributes_with_associations
} }
render json: users_all, status: :ok render json: users_all, status: :ok
end end
@ -45,12 +45,12 @@ class UsersController < ApplicationController
return if !permission_check return if !permission_check
if params[:full] if params[:full]
full = User.full( params[:id] ) full = User.full(params[:id])
render json: full render json: full
return return
end end
user = User.find( params[:id] ) user = User.find(params[:id])
render json: user render json: user
end end
@ -85,7 +85,7 @@ class UsersController < ApplicationController
group_ids = [] group_ids = []
role_ids = [] role_ids = []
if count <= 2 if count <= 2
Role.where( name: [ Z_ROLENAME_ADMIN, 'Agent', 'Chat'] ).each { |role| Role.where(name: [ Z_ROLENAME_ADMIN, 'Agent', 'Chat']).each { |role|
role_ids.push role.id role_ids.push role.id
} }
Group.all().each { |group| Group.all().each { |group|
@ -94,7 +94,7 @@ class UsersController < ApplicationController
# everybody else will go as customer per default # everybody else will go as customer per default
else else
role_ids.push Role.where( name: Z_ROLENAME_CUSTOMER ).first.id role_ids.push Role.where(name: Z_ROLENAME_CUSTOMER).first.id
end end
user.role_ids = role_ids user.role_ids = role_ids
user.group_ids = group_ids user.group_ids = group_ids
@ -115,18 +115,17 @@ class UsersController < ApplicationController
# check if user already exists # check if user already exists
if user.email if user.email
exists = User.where( email: user.email ).first exists = User.where(email: user.email.downcase).first
if exists if exists
render json: { error: 'User already exists!' }, status: :unprocessable_entity render json: { error: 'User already exists!' }, status: :unprocessable_entity
return return
end end
end end
user.save! user.save!
# if first user was added, set system init done # if first user was added, set system init done
if count <= 2 if count <= 2
Setting.set( 'system_init_done', true ) Setting.set('system_init_done', true)
# fetch org logo # fetch org logo
if user.email if user.email
@ -138,7 +137,7 @@ class UsersController < ApplicationController
if params[:invite] && current_user if params[:invite] && current_user
# generate token # generate token
token = Token.create( action: 'PasswordReset', user_id: user.id ) token = Token.create(action: 'PasswordReset', user_id: user.id)
# send mail # send mail
data = {} data = {}
@ -178,8 +177,7 @@ class UsersController < ApplicationController
body: data[:body] body: data[:body]
) )
end end
user_new = User.find(user.id)
user_new = User.find( user.id )
render json: user_new, status: :created render json: user_new, status: :created
rescue => e rescue => e
render json: { error: e.message }, status: :unprocessable_entity render json: { error: e.message }, status: :unprocessable_entity
@ -201,7 +199,7 @@ class UsersController < ApplicationController
# access deny # access deny
return if !permission_check return if !permission_check
user = User.find( params[:id] ) user = User.find(params[:id])
begin begin
@ -396,7 +394,7 @@ class UsersController < ApplicationController
end end
# get user data # get user data
user = User.find( params[:id] ) user = User.find(params[:id])
# get history of user # get history of user
history = user.history_get(true) history = user.history_get(true)
@ -433,7 +431,7 @@ curl http://localhost/api/v1/users/password_reset.json -v -u #{login}:#{password
return return
end end
token = User.password_reset_send( params[:username] ) token = User.password_reset_send(params[:username])
if token if token
# only if system is in develop mode, send token back to browser for browser tests # only if system is in develop mode, send token back to browser for browser tests
@ -483,9 +481,9 @@ curl http://localhost/api/v1/users/password_reset_verify.json -v -u #{login}:#{p
end end
# set new password with token # set new password with token
user = User.password_reset_via_token( params[:token], params[:password] ) user = User.password_reset_via_token(params[:token], params[:password])
else else
user = User.password_reset_check( params[:token] ) user = User.password_reset_check(params[:token])
end end
if user if user
render json: { message: 'ok', user_login: user.login }, status: :ok render json: { message: 'ok', user_login: user.login }, status: :ok
@ -541,7 +539,7 @@ curl http://localhost/api/v1/users/password_change.json -v -u #{login}:#{passwor
return return
end end
user.update_attributes( password: params[:password_new] ) user.update_attributes(password: params[:password_new])
render json: { message: 'ok', user_login: user.login }, status: :ok render json: { message: 'ok', user_login: user.login }, status: :ok
end end
@ -652,7 +650,7 @@ curl http://localhost/api/v1/users/image/8d6cca1c6bdc226cf2ba131e264ca2c7 -v -u
response.headers['Cache-Control'] = 'cache, store, max-age=31536000, must-revalidate' response.headers['Cache-Control'] = 'cache, store, max-age=31536000, must-revalidate'
response.headers['Pragma'] = 'cache' response.headers['Pragma'] = 'cache'
file = Avatar.get_by_hash( params[:hash] ) file = Avatar.get_by_hash(params[:hash])
if file if file
send_data( send_data(
file.content, file.content,
@ -685,7 +683,7 @@ Payload:
Response: Response:
{ {
:message => 'ok' message: 'ok'
} }
Test: Test:
@ -697,8 +695,8 @@ curl http://localhost/api/v1/users/avatar -v -u #{login}:#{password} -H "Content
return if !valid_session_with_user return if !valid_session_with_user
# get & validate image # get & validate image
file_full = StaticAssets.data_url_attributes( params[:avatar_full] ) file_full = StaticAssets.data_url_attributes(params[:avatar_full])
file_resize = StaticAssets.data_url_attributes( params[:avatar_resize] ) file_resize = StaticAssets.data_url_attributes(params[:avatar_resize])
avatar = Avatar.add( avatar = Avatar.add(
object: 'User', object: 'User',
@ -716,7 +714,7 @@ curl http://localhost/api/v1/users/avatar -v -u #{login}:#{password} -H "Content
) )
# update user link # update user link
current_user.update_attributes( image: avatar.store_hash ) current_user.update_attributes(image: avatar.store_hash)
render json: { avatar: avatar }, status: :ok render json: { avatar: avatar }, status: :ok
end end
@ -731,10 +729,10 @@ curl http://localhost/api/v1/users/avatar -v -u #{login}:#{password} -H "Content
end end
# set as default # set as default
avatar = Avatar.set_default( 'User', current_user.id, params[:id] ) avatar = Avatar.set_default('User', current_user.id, params[:id])
# update user link # update user link
current_user.update_attributes( image: avatar.store_hash ) current_user.update_attributes(image: avatar.store_hash)
render json: {}, status: :ok render json: {}, status: :ok
end end
@ -749,11 +747,11 @@ curl http://localhost/api/v1/users/avatar -v -u #{login}:#{password} -H "Content
end end
# remove avatar # remove avatar
Avatar.remove_one( 'User', current_user.id, params[:id] ) Avatar.remove_one('User', current_user.id, params[:id])
# update user link # update user link
avatar = Avatar.get_default( 'User', current_user.id ) avatar = Avatar.get_default('User', current_user.id)
current_user.update_attributes( image: avatar.store_hash ) current_user.update_attributes(image: avatar.store_hash)
render json: {}, status: :ok render json: {}, status: :ok
end end
@ -762,7 +760,7 @@ curl http://localhost/api/v1/users/avatar -v -u #{login}:#{password} -H "Content
return if !valid_session_with_user return if !valid_session_with_user
# list of avatars # list of avatars
result = Avatar.list( 'User', current_user.id ) result = Avatar.list('User', current_user.id)
render json: { avatars: result }, status: :ok render json: { avatars: result }, status: :ok
end end

View file

@ -35,9 +35,9 @@ returns if user has no permissions to search
search organizations search organizations
result = Organization.search( result = Organization.search(
:current_user => User.find(123), current_user: User.find(123),
:query => 'search something', query: 'search something',
:limit => 15, limit: 15,
) )
returns returns
@ -58,10 +58,10 @@ returns
# try search index backend # try search index backend
if SearchIndexBackend.enabled? if SearchIndexBackend.enabled?
items = SearchIndexBackend.search( query, limit, 'Organization' ) items = SearchIndexBackend.search(query, limit, 'Organization')
organizations = [] organizations = []
items.each { |item| items.each { |item|
organizations.push Organization.lookup( id: item[:id] ) organizations.push Organization.lookup(id: item[:id])
} }
return organizations return organizations
end end
@ -75,7 +75,7 @@ returns
# if only a few organizations are found, search for names of users # if only a few organizations are found, search for names of users
if organizations.length <= 3 if organizations.length <= 3
organizations_by_user = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where( organizations_by_user = Organization.select('DISTINCT(organizations.id), organizations.name').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where(
'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%" 'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"
).order('organizations.name').limit(limit) ).order('organizations.name').limit(limit)
organizations_by_user.each {|organization_by_user| organizations_by_user.each {|organization_by_user|

View file

@ -104,8 +104,8 @@ returns
if current_user.role?('Agent') if current_user.role?('Agent')
groups = Group.joins(:users) groups = Group.joins(:users)
.where( 'groups_users.user_id = ?', current_user.id ) .where('groups_users.user_id = ?', current_user.id)
.where( 'groups.active = ?', true ) .where('groups.active = ?', true)
group_condition = [] group_condition = []
groups.each {|group| groups.each {|group|
group_condition.push group.name group_condition.push group.name
@ -131,7 +131,7 @@ returns
query_extention['bool']['must'].push access_condition query_extention['bool']['must'].push access_condition
items = SearchIndexBackend.search( query, limit, 'Ticket', query_extention ) items = SearchIndexBackend.search(query, limit, 'Ticket', query_extention)
if !full if !full
ids = [] ids = []
items.each {|item| items.each {|item|
@ -141,30 +141,30 @@ returns
end end
tickets = [] tickets = []
items.each { |item| items.each { |item|
tickets.push Ticket.lookup( id: item[:id] ) tickets.push Ticket.lookup(id: item[:id])
} }
return tickets return tickets
end end
# fallback do sql query # fallback do sql query
access_condition = Ticket.access_condition( current_user ) access_condition = Ticket.access_condition(current_user)
# do query # do query
# - stip out * we already search for *query* - # - stip out * we already search for *query* -
if query if query
query.delete! '*' query.delete! '*'
tickets_all = Ticket.select('DISTINCT(tickets.id)') tickets_all = Ticket.select('DISTINCT(tickets.id), tickets.created_at')
.where(access_condition) .where(access_condition)
.where( '( `tickets`.`title` LIKE ? OR `tickets`.`number` LIKE ? OR `ticket_articles`.`body` LIKE ? OR `ticket_articles`.`from` LIKE ? OR `ticket_articles`.`to` LIKE ? OR `ticket_articles`.`subject` LIKE ?)', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%" ) .where('(tickets.title LIKE ? OR tickets.number LIKE ? OR ticket_articles.body LIKE ? OR ticket_articles.from LIKE ? OR ticket_articles.to LIKE ? OR ticket_articles.subject LIKE ?)', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%" )
.joins(:articles) .joins(:articles)
.order('`tickets`.`created_at` DESC') .order('tickets.created_at DESC')
.limit(limit) .limit(limit)
else else
query_condition, bind_condition = selector2sql(params[:condition]) query_condition, bind_condition = selector2sql(params[:condition])
tickets_all = Ticket.select('DISTINCT(tickets.id)') tickets_all = Ticket.select('DISTINCT(tickets.id), tickets.created_at')
.where(access_condition) .where(access_condition)
.where(query_condition, *bind_condition) .where(query_condition, *bind_condition)
.order('`tickets`.`created_at` DESC') .order('tickets.created_at DESC')
.limit(limit) .limit(limit)
end end
@ -179,7 +179,7 @@ returns
tickets = [] tickets = []
tickets_all.each { |ticket| tickets_all.each { |ticket|
tickets.push Ticket.lookup( id: ticket.id ) tickets.push Ticket.lookup(id: ticket.id)
} }
tickets tickets
end end