iugusdk 1.0.0.alpha.1 → 1.0.0.alpha.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/app/controllers/iugu/account_controller.rb +24 -28
  2. data/app/controllers/iugu/account_domains_controller.rb +35 -5
  3. data/app/controllers/iugu/account_roles_controller.rb +2 -5
  4. data/app/controllers/iugu/account_users_controller.rb +6 -12
  5. data/app/controllers/iugu/confirmations_controller.rb +1 -0
  6. data/app/controllers/iugu/invitations_controller.rb +17 -23
  7. data/app/controllers/iugu/omniauth_callbacks_controller.rb +3 -1
  8. data/app/controllers/iugu/passwords_controller.rb +1 -0
  9. data/app/controllers/iugu/registrations_controller.rb +11 -0
  10. data/app/controllers/iugu/sessions_controller.rb +1 -0
  11. data/app/controllers/iugu/settings_controller.rb +14 -0
  12. data/app/mailers/iugu_mailer.rb +2 -2
  13. data/app/models/account.rb +15 -0
  14. data/app/models/account_domain.rb +25 -2
  15. data/app/models/user.rb +19 -3
  16. data/app/models/user_invitation.rb +15 -2
  17. data/app/views/iugu/account_domains/index.html.haml +50 -25
  18. data/app/views/iugu/account_domains/instructions.html.haml +3 -0
  19. data/app/views/iugu/account_users/index.html.haml +4 -2
  20. data/app/views/iugu/invitations/new.html.haml +19 -5
  21. data/app/views/iugu/registrations/new.html.haml +4 -1
  22. data/app/views/iugu/sessions/new.html.haml +9 -8
  23. data/app/views/iugu/settings/account.html.haml +31 -23
  24. data/app/views/iugu/settings/accounts.html.haml +5 -3
  25. data/app/views/iugu/settings/profile.html.haml +47 -44
  26. data/app/views/iugu/shared/_links.haml +14 -11
  27. data/app/views/layouts/settings.html.haml +1 -1
  28. data/config/initializers/devise.rb +0 -13
  29. data/config/locales/iugu.en.yml +6 -0
  30. data/config/locales/iugu.pt-BR.yml +6 -0
  31. data/config/routes.rb +9 -4
  32. data/db/migrate/20120725170859_add_api_token_to_account.rb +9 -0
  33. data/db/migrate/20120803172545_add_guest_to_users.rb +9 -0
  34. data/lib/iugusdk/controllers/helpers.rb +1 -1
  35. data/lib/iugusdk/engine.rb +1 -1
  36. data/lib/iugusdk/iugusdk_base_controller.rb +17 -0
  37. data/lib/iugusdk/version.rb +1 -1
  38. data/lib/iugusdk.rb +30 -5
  39. data/spec/controller_macros.rb +1 -1
  40. data/spec/controllers/account_controller_spec.rb +26 -45
  41. data/spec/controllers/account_domains_controller_spec.rb +87 -5
  42. data/spec/controllers/account_roles_controller_spec.rb +0 -14
  43. data/spec/controllers/invitations_controller_spec.rb +21 -1
  44. data/spec/controllers/registration_controller_spec.rb +15 -0
  45. data/spec/controllers/settings_controller_spec.rb +105 -0
  46. data/spec/dummy/db/schema.rb +3 -1
  47. data/spec/dummy/log/development.log +1579 -0
  48. data/spec/dummy/tmp/cache/assets/C91/3F0/sprockets%2Fcd844928d19e586b4146833b720e7d29 +0 -0
  49. data/spec/dummy/tmp/cache/assets/CAA/190/sprockets%2Fee7298fdc707d359368983e09687c627 +0 -0
  50. data/spec/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  51. data/spec/dummy/tmp/cache/assets/CE6/A00/sprockets%2F583256e44957e430e1eca9ba1739b6b6 +0 -0
  52. data/spec/dummy/tmp/cache/assets/CE7/4B0/sprockets%2Fd3774b9099289a36db44caec70813a19 +0 -0
  53. data/spec/dummy/tmp/cache/assets/CF1/A70/sprockets%2F368a631d9662bcc4891c91f739b7be37 +0 -0
  54. data/spec/dummy/tmp/cache/assets/CF6/A50/sprockets%2Ff434c1ed5d55916f790cf698832f76b1 +0 -0
  55. data/spec/dummy/tmp/cache/assets/CFB/F30/sprockets%2F84131db318a2faf415628c1075c32bdd +0 -0
  56. data/spec/dummy/tmp/cache/assets/D1A/F20/sprockets%2F102cc77ec78847746b1c164edcb9b639 +0 -0
  57. data/spec/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  58. data/spec/dummy/tmp/cache/assets/D3A/D70/sprockets%2Fa3bc2dd9e66062ea36222fc6154827ee +0 -0
  59. data/spec/dummy/tmp/cache/assets/D46/E20/sprockets%2F1de16fe0c12f9988664ed66a835a6ab0 +0 -0
  60. data/spec/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  61. data/spec/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  62. data/spec/dummy/tmp/cache/assets/D64/F60/sprockets%2F4755d7f5deef0f3212cb7ae30172a33e +0 -0
  63. data/spec/dummy/tmp/cache/assets/D69/850/sprockets%2F5c160a5a6d1068a17af57ab38affd379 +0 -0
  64. data/spec/dummy/tmp/cache/assets/D77/1A0/sprockets%2Ff3864fc006a0674b4c977ff5ca5c7d2e +0 -0
  65. data/spec/dummy/tmp/cache/assets/D77/920/sprockets%2Faf0e746c541e6cf4540db92c87da579c +0 -0
  66. data/spec/dummy/tmp/cache/assets/D7A/850/sprockets%2Fd1e5bf9f64829d062ed7314af28a4e7e +0 -0
  67. data/spec/dummy/tmp/cache/assets/D86/3F0/sprockets%2F8f648e11dfcec044b2997b98f619cfa6 +0 -0
  68. data/spec/dummy/tmp/cache/assets/DA5/130/sprockets%2F7d741fa9ebe86e96fb2bd116ca91234e +0 -0
  69. data/spec/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  70. data/spec/dummy/tmp/cache/assets/DF2/910/sprockets%2F02db4a9e6f91b47261fa5b0eece2ab8b +0 -0
  71. data/spec/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  72. data/spec/dummy/tmp/cache/assets/E25/5A0/sprockets%2F795f5759cf9bc7cde9f8acf8c0ca479f +0 -0
  73. data/spec/dummy/tmp/cache/assets/E35/0E0/sprockets%2F9f57eaeefd4c9d5efb41c9801e3f1c1a +0 -0
  74. data/spec/dummy/tmp/cache/sass/e5be18dde92936a4632e65289dad5788ed73dd60/settings.sassc +0 -0
  75. data/spec/dummy/tmp/cache/sass/e764476e9a85279ad82622591ce49983ed21c149/default.sassc +0 -0
  76. data/spec/fabricators/account_domain_fabricator.rb +5 -1
  77. data/spec/models/account_domain_spec.rb +49 -12
  78. data/spec/models/account_spec.rb +32 -4
  79. data/spec/models/user_invitation_spec.rb +23 -1
  80. data/spec/models/user_spec.rb +76 -0
  81. data/spec/requests/account_domain_spec.rb +71 -37
  82. data/spec/requests/account_roles_spec.rb +1 -0
  83. data/spec/requests/account_spec.rb +65 -3
  84. data/spec/requests/account_users_spec.rb +1 -0
  85. data/spec/requests/omniauth_spec.rb +34 -0
  86. data/spec/requests/settings_spec.rb +4 -4
  87. data/spec/requests/user_invitation_spec.rb +32 -1
  88. data/spec/requests/user_spec.rb +13 -0
  89. metadata +126 -70
  90. data/lib/iugusdk/locale_filter.rb +0 -12
@@ -1,4 +1,7 @@
1
1
  class Iugu::AccountController < Iugu::AccountSettingsController
2
+
3
+ before_filter(:only => [:destroy, :cancel_destruction, :update]) { |c| c.must_be :owner, :id }
4
+ before_filter(:only => [:generate_new_token]) { |c| c.must_be :owner, :account_id }
2
5
 
3
6
  def index
4
7
  render 'iugu/settings/accounts'
@@ -8,51 +11,33 @@ class Iugu::AccountController < Iugu::AccountSettingsController
8
11
  if params[:id]
9
12
  @account = current_user.accounts.find(params[:id])
10
13
  else
11
- @account = current_user_account
14
+ @account = current_user_account.account
12
15
  end
13
16
  @primary_domain = @account.account_domains.where(:primary => true).first if @account
14
17
  render 'iugu/settings/account'
15
18
  end
16
19
 
17
20
  def destroy
18
- begin
19
- if account = current_user.accounts.find(params[:id])
20
- if account.account_users.find_by_user_id(current_user.id).is?(:owner)
21
- account.destroy
22
- notice = I18n.t("iugu.account_destruction_in") + account.destruction_job.run_at.to_s
23
- else
24
- notice = I18n.t("errors.messages.only_owners_can_destroy_accounts")
25
- end
26
- end
27
- rescue
28
- notice = I18n.t("iugu.notices.account_not_found")
29
- end
30
- redirect_to(account_settings_path, :notice => notice)
21
+ account = current_user.accounts.find(params[:id])
22
+ account.destroy
23
+ redirect_to(account_settings_path, :notice => I18n.t("iugu.account_destruction_in") + account.destruction_job.run_at.to_s)
31
24
  end
32
25
 
33
26
  def cancel_destruction
34
- begin
35
- current_user.accounts.find(params[:id]).cancel_destruction if params[:id]
36
- notice = I18n.t("iugu.account_destruction_undone")
37
- rescue
38
- notice = I18n.t("iugu.notices.account_not_found")
39
- end
27
+ current_user.accounts.find(params[:id]).cancel_destruction
28
+ notice = I18n.t("iugu.account_destruction_undone")
40
29
  redirect_to(account_settings_path, :notice => notice)
41
30
  end
42
31
 
43
32
  def select
44
- select_account(current_user, params[:id])
33
+ set_account(current_user, params[:id])
45
34
  redirect_to(account_settings_path, :notice => "Account selected")
46
35
  end
47
36
 
48
37
  def update
49
- begin
50
- @account = Account.find(params[:id])
51
- @account.update_attributes(params[:account])
52
- redirect_to account_view_path(params[:id]), :notice => I18n.t("iugu.notices.account_updated")
53
- rescue
54
- raise ActionController::RoutingError.new('Not Found')
55
- end
38
+ @account = Account.find(params[:id])
39
+ @account.update_attributes(params[:account])
40
+ redirect_to account_view_path(params[:id]), :notice => I18n.t("iugu.notices.account_updated")
56
41
  end
57
42
 
58
43
  def create
@@ -60,4 +45,15 @@ class Iugu::AccountController < Iugu::AccountSettingsController
60
45
  redirect_to account_settings_path
61
46
  end
62
47
 
48
+ def generate_new_token
49
+ if IuguSDK::enable_account_api_token == true
50
+ @account = current_user.accounts.find(params[:account_id])
51
+ @account.update_api_token
52
+ redirect_to account_view_path(params[:account_id]), :notice => I18n.t("iugu.notices.new_token_generated")
53
+ else
54
+ raise ActionController::RoutingError.new('Not found')
55
+ end
56
+
57
+ end
58
+
63
59
  end
@@ -1,16 +1,25 @@
1
1
  class Iugu::AccountDomainsController < Iugu::AccountSettingsController
2
2
 
3
+ before_filter :custom_domain_enabled?, :except => [:update_subdomain, :index]
4
+
5
+ before_filter(:only => [:index, :create, :destroy, :instruction, :verify, :primary, :update_subdomain]) { |c| c.must_be [:owner, :admin], :account_id }
6
+
3
7
  def index
4
- @account = current_user.accounts.find(params[:account_id])
5
- @account_domains = @account.account_domains.where(:account_id => params[:account_id])
6
- @account_domain = AccountDomain.new
8
+ unless IuguSDK::enable_custom_domain == false && IuguSDK::enable_subdomain == false
9
+ @account = current_user.accounts.find(params[:account_id])
10
+ @account_domains = @account.account_domains.where(:account_id => params[:account_id])
11
+ @account_domain = AccountDomain.new
12
+ else
13
+ raise ActionController::RoutingError.new("Not found")
14
+ end
7
15
  end
8
16
 
9
17
  def create
10
18
  @account = current_user.accounts.find(params[:account_id])
11
- if @account.account_domains << @domain = AccountDomain.create(params[:account_domain])
12
- redirect_to account_domains_instructions_path(:account_id => params[:account_id], :domain_id => @domain.id)#, :notice => notice = I18n.t("iugu.notices.domain_created")
19
+ if @account.account_domains << @account_domain = AccountDomain.create(params[:account_domain])
20
+ redirect_to account_domains_instructions_path(:account_id => params[:account_id], :domain_id => @account_domain.id)#, :notice => notice = I18n.t("iugu.notices.domain_created")
13
21
  else
22
+ @account_domains = @account.account_domains.where(:account_id => params[:account_id])
14
23
  render :index
15
24
  end
16
25
  end
@@ -60,5 +69,26 @@ class Iugu::AccountDomainsController < Iugu::AccountSettingsController
60
69
  redirect_to account_domains_index_path(params[:account_id]), :notice => I18n.t("iugu.notices.domain_not_found")
61
70
  end
62
71
  end
72
+
73
+ def update_subdomain
74
+ if IuguSDK::enable_subdomain == true
75
+ @account = current_user.accounts.find(params[:account_id])
76
+ if @account.update_attributes(params[:account])
77
+ redirect_to account_domains_index_path(@account.id), :notice => I18n.t("iugu.notices.subdomain_updated")
78
+ else
79
+ @account_domains = @account.account_domains.where(:account_id => params[:account_id])
80
+ @account_domain = AccountDomain.new
81
+ render :index
82
+ end
83
+ else
84
+ raise ActionController::RoutingError.new('Not found')
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def custom_domain_enabled?
91
+ raise ActionController::RoutingError.new('Not found') if IuguSDK::enable_custom_domain == false
92
+ end
63
93
 
64
94
  end
@@ -1,13 +1,10 @@
1
1
  class Iugu::AccountRolesController < Iugu::SettingsController
2
2
 
3
+ before_filter(:only => [:edit, :update]) { |c| c.must_be [:owner, :admin], :id }
4
+
3
5
  def edit
4
6
  @account = current_user.accounts.find(params[:id])
5
7
  @account_user = @account.account_users.find_by_user_id(params[:user_id])
6
- if current_user.is?(:owner, @account) || current_user.is?(:admin, @account)
7
- render 'iugu/account_roles/edit'
8
- else
9
- render :file => "#{Rails.root}/public/422.html", :status => 550
10
- end
11
8
  end
12
9
 
13
10
  def update
@@ -1,5 +1,7 @@
1
1
  class Iugu::AccountUsersController < Iugu::AccountSettingsController
2
2
 
3
+ before_filter(:only => [:destroy, :cancel_destruction]) { |c| c.must_be [:owner, :admin], :account_id }
4
+
3
5
  def index
4
6
  @account = Account.find(params[:account_id])
5
7
  @account_users = @account.account_users
@@ -17,24 +19,16 @@ class Iugu::AccountUsersController < Iugu::AccountSettingsController
17
19
  else
18
20
  raise ActionController::RoutingError.new('Not Found')
19
21
  end
20
- if current_user.is?(:owner, @account) || current_user.is?(:owner, @account)
21
- raise ActionController::RoutingError.new('Access Denied') if @account_user.user_id == current_user.id || @account_user.is?(:owner)
22
- @account_user.destroy
23
- else
24
- raise ActionController::RoutingError.new('Access Denied')
25
- end
22
+ raise ActionController::RoutingError.new('Access Denied') if @account_user.user_id == current_user.id || @account_user.is?(:owner)
23
+ @account_user.destroy
26
24
  redirect_to account_users_index_path(params[:account_id]), :notice => I18n.t("iugu.account_user_destruction_in") + @account_user.destruction_job.run_at.to_s
27
25
  end
28
26
 
29
27
  def cancel_destruction
30
28
  if @account_user = AccountUser.find_by_account_id_and_user_id(params[:account_id], params[:user_id])
31
29
  @account = Account.find(params[:account_id])
32
- if current_user.is?(:owner, @account) || current_user.is?(:owner, @account)
33
- @account_user.cancel_destruction
34
- redirect_to account_users_index_path(params[:account_id]), :notice => I18n.t("iugu.account_user_destruction_undone")
35
- else
36
- raise ActionController::RoutingError.new('Access Denied')
37
- end
30
+ @account_user.cancel_destruction
31
+ redirect_to account_users_index_path(params[:account_id]), :notice => I18n.t("iugu.account_user_destruction_undone")
38
32
  else
39
33
  raise ActionController::RoutingError.new('Not Found')
40
34
  end
@@ -1,2 +1,3 @@
1
1
  class Iugu::ConfirmationsController < Devise::ConfirmationsController
2
+ after_filter :select_account, :only => :show
2
3
  end
@@ -1,18 +1,23 @@
1
1
  class Iugu::InvitationsController < Iugu::SettingsController
2
- before_filter :check_permissions
2
+
3
+ before_filter(:only => [:new, :create]) { |c| c.must_be [:owner, :admin], :account_id }
3
4
 
4
5
  def new
5
6
  @user_invitation = UserInvitation.new
6
- @account_id = params[:account_id]
7
+ @account = Account.find(params[:account_id])
7
8
  end
8
9
 
9
10
  def create
10
11
  params[:user_invitation][:roles] = params[:user_invitation][:roles].try(:join, ',')
11
- @user_invitation = UserInvitation.new(params[:user_invitation])
12
- @user_invitation.invited_by = current_user.id
13
- @user_invitation.account_id = params[:account_id]
14
- @user_invitation.save
15
- redirect_to account_users_index_path(params[:account_id]), :notice => I18n.t("iugu.notices.user_invited")
12
+ params[:user_invitation][:account_id] = params[:account_id]
13
+ params[:user_invitation][:invited_by] = current_user.id
14
+ @user_invitation = UserInvitation.create(params[:user_invitation])
15
+ unless @user_invitation.new_record?
16
+ redirect_to account_users_index_path(params[:account_id]), :notice => I18n.t("iugu.notices.user_invited")
17
+ else
18
+ @account = Account.find(params[:account_id])
19
+ render :new
20
+ end
16
21
  end
17
22
 
18
23
  def edit
@@ -25,25 +30,14 @@ class Iugu::InvitationsController < Iugu::SettingsController
25
30
 
26
31
  def update
27
32
  if @user_invitation = UserInvitation.find_by_invitation_token(params[:invitation_token])
28
- @user_invitation.accept(current_user)
29
- redirect_to root_path
33
+ if @user_invitation.accept(current_user)
34
+ redirect_to root_path
35
+ else
36
+ redirect_to root_path, :notice => I18n.t("iugu.notices.you_are_already_member_of_this_account")
37
+ end
30
38
  else
31
39
  raise ActionController::RoutingError.new('Not Found')
32
40
  end
33
41
  end
34
42
 
35
-
36
- private
37
-
38
- def check_permissions
39
- if params[:account_id]
40
- begin
41
- account = current_user.accounts.find(params[:account_id])
42
- rescue
43
- raise ActionController::RoutingError.new('Access denied')
44
- end
45
- raise ActionController::RoutingError.new('Access denied') unless current_user.is?(:owner, account) || current_user.is?(:admin, account)
46
- end
47
- end
48
-
49
43
  end
@@ -3,12 +3,14 @@ class Iugu::OmniauthCallbacksController < Devise::OmniauthCallbacksController
3
3
  def method_missing(provider)
4
4
  if !User.omniauth_providers.index(provider).nil?
5
5
  if current_user
6
+ raise ActionController::RoutingError.new("Not found") unless IuguSDK::enable_social_linking
6
7
  current_user.find_or_create_social(env["omniauth.auth"])
7
8
  redirect_to after_sign_in_path_for( current_user )
8
9
  else
10
+ raise ActionController::RoutingError.new("Not found") unless IuguSDK::enable_social_login
9
11
  if user = User.find_or_create_by_social(env["omniauth.auth"])
10
- select_account user
11
12
  sign_in user
13
+ select_account
12
14
  redirect_to after_sign_in_path_for( user )
13
15
  else
14
16
  redirect_to (env["omniauth.origin"] || root_path), :notice => I18n.t('errors.messages.email_already_in_use')
@@ -1,2 +1,3 @@
1
1
  class Iugu::PasswordsController < Devise::PasswordsController
2
+ after_filter :select_account, :only => :update
2
3
  end
@@ -1,3 +1,14 @@
1
1
  class Iugu::RegistrationsController < Devise::RegistrationsController
2
+ after_filter :select_account, :only => [:create,:update]
3
+
4
+ def try_first
5
+ @user = User.create_guest
6
+ @user.remember_me = true
7
+ sign_in @user
8
+ select_account
9
+ flash[:notice] = I18n.t("iugu.notices.guest_login")
10
+ redirect_to root_path
11
+ #respond_with @user, :location => sign_up_path_for(@user)
12
+ end
2
13
  end
3
14
 
@@ -1,2 +1,3 @@
1
1
  class Iugu::SessionsController < Devise::SessionsController
2
+ after_filter :select_account, :only => :create
2
3
  end
@@ -8,4 +8,18 @@ class Iugu::SettingsController < ApplicationController
8
8
  redirect_to :profile_settings, :notice => flash[:notice]
9
9
  end
10
10
 
11
+ def must_be (roles, param_name)
12
+ access = false
13
+ @account_user = AccountUser.find_by_user_id_and_account_id(current_user.id, params[param_name])
14
+ raise ActionController::RoutingError.new("Not Found") unless @account_user
15
+ if roles.class == Array
16
+ roles.each do |role|
17
+ access = true if @account_user.is?(role)
18
+ end
19
+ else
20
+ access = true if @account_user.is?(roles)
21
+ end
22
+ raise ActionController::RoutingError.new("Access Denied") if access == false
23
+ end
24
+
11
25
  end
@@ -1,7 +1,7 @@
1
1
  class IuguMailer < Devise::Mailer
2
2
 
3
- default from: "Kupz <equipe@kupz.com.br>",
4
- reply_to: "Kupz <atendimento@kupz.com.br>"
3
+ default from: "Iugu <equipe@iugu.com.br>",
4
+ reply_to: "Iugu <atendimento@iugu.com.br>"
5
5
 
6
6
  def template_paths
7
7
  "iugu/mailer"
@@ -8,8 +8,11 @@ class Account < ActiveRecord::Base
8
8
  :run_at => Proc.new { DateTime.now + IuguSDK::delay_account_exclusion }
9
9
 
10
10
  validates :subdomain, :uniqueness => true, :unless => Proc.new { |a| a.subdomain.blank? }
11
+ validates :api_token, :uniqueness => true, :if => :api_token?
11
12
  validate :subdomain_blacklist
12
13
 
14
+ before_create :set_first_token
15
+
13
16
  def self.get_from_domain(domain)
14
17
  AccountDomain.verified.find_by_url(domain).try(:account) || Account.find_by_subdomain(domain.gsub(".#{IuguSDK::application_main_host}",""))
15
18
  end
@@ -39,9 +42,21 @@ class Account < ActiveRecord::Base
39
42
  def name
40
43
  (super.blank? ? "#{I18n.t('iugu.account')} ##{id}" : super)
41
44
  end
45
+
46
+ def update_api_token
47
+ self.update_attribute(:api_token, generate_api_token)
48
+ end
42
49
 
43
50
  private
44
51
 
52
+ def set_first_token
53
+ self.api_token = generate_api_token
54
+ end
55
+
56
+ def generate_api_token
57
+ Digest::MD5.hexdigest("#{SecureRandom.hex(10)}-#{DateTime.now.to_s}")
58
+ end
59
+
45
60
  def subdomain_blacklist
46
61
  if subdomain
47
62
  IuguSDK::custom_domain_invalid_prefixes.each do |invalid_prefix|
@@ -5,6 +5,11 @@ class AccountDomain < ActiveRecord::Base
5
5
  validates :url, :account_id, :presence => true
6
6
  validate :validate_pattern, :validate_blacklist
7
7
 
8
+ before_create :validate_not_repeated
9
+
10
+ before_destroy { |record| record.update_attributes(:verified => false, :primary => false) }
11
+ before_destroy :set_first_domain
12
+
8
13
  scope :verified, where(:verified => true)
9
14
 
10
15
  def normalize_host
@@ -41,12 +46,15 @@ class AccountDomain < ActiveRecord::Base
41
46
  end
42
47
  AccountDomain.where(:url => self.url).update_all(:verified => false) if checked == true
43
48
  update_attribute(:verified, checked)
49
+ set_first_domain
44
50
  checked
45
51
  end
46
52
 
47
53
  def set_primary
48
- AccountDomain.where(:account_id => account_id).update_all(:primary => false)
49
- update_attribute(:primary, true)
54
+ if verified == true
55
+ AccountDomain.where(:account_id => account_id).update_all(:primary => false)
56
+ update_attribute(:primary, true)
57
+ end
50
58
  end
51
59
 
52
60
  private
@@ -64,6 +72,21 @@ class AccountDomain < ActiveRecord::Base
64
72
  end
65
73
  end
66
74
  end
75
+
76
+ def validate_not_repeated
77
+ if url
78
+ if !AccountDomain.where(:account_id => account_id, :url => url).empty?
79
+ errors.add(:url, "already used for this account")
80
+ false
81
+ end
82
+ end
83
+ end
84
+
85
+ def set_first_domain
86
+ if AccountDomain.where(:account_id => account_id, :primary => true).empty?
87
+ AccountDomain.where(:account_id => account_id, :verified => true).first.try(:update_attribute, :primary, true)
88
+ end
89
+ end
67
90
 
68
91
 
69
92
  end
data/app/models/user.rb CHANGED
@@ -14,11 +14,15 @@ class User < ActiveRecord::Base
14
14
  :recoverable, :rememberable, :validatable, :omniauthable
15
15
 
16
16
  # Setup accessible (or protected) attributes for your model
17
- attr_accessible :email, :password, :password_confirmation, :remember_me, :locale, :name, :birthdate
17
+ attr_accessible :email, :password, :password_confirmation, :remember_me, :locale, :name, :birthdate, :guest
18
+
19
+ before_create :skip_confirmation!, :unless => Proc.new { IuguSDK::enable_user_confirmation }
18
20
 
19
21
  after_create :create_account_for_user
20
22
 
21
- validates :email, :email => true
23
+ before_save :skip_reconfirmation!, :unless => Proc.new { IuguSDK::enable_email_reconfirmation }
24
+
25
+ validates :email, :email => true, :unless => :guest?
22
26
 
23
27
  def destruction_job
24
28
  Delayed::Job.find_by_queue("user_#{id}_destroy")
@@ -44,6 +48,17 @@ class User < ActiveRecord::Base
44
48
  social_accounts.where("provider = ? AND social_id = ?", auth["provider"], auth["uid"]).first || create_social(auth)
45
49
  end
46
50
 
51
+ def self.create_guest(locale = "en")
52
+ user = User.new({
53
+ :guest => true,
54
+ :name => "Guest",
55
+ :locale => locale
56
+ })
57
+ user.skip_confirmation!
58
+ user.save
59
+ user
60
+ end
61
+
47
62
  def self.find_or_create_by_social(auth)
48
63
  social_account = SocialAccount.where("provider = ? AND social_id = ?", auth["provider"], auth["uid"]).first
49
64
  unless user = social_account.try(:user)
@@ -89,9 +104,10 @@ class User < ActiveRecord::Base
89
104
  private
90
105
 
91
106
  def email_required?
92
- !has_social?
107
+ !(has_social? || guest?)
93
108
  end
94
109
 
110
+
95
111
  #def self.reconfirmable
96
112
  # true
97
113
  #end
@@ -1,5 +1,6 @@
1
1
  class UserInvitation < ActiveRecord::Base
2
2
  validates :email, :email => true, :presence => true
3
+ validate :email_already_used?
3
4
  before_save :set_token
4
5
  before_create :set_sent_at
5
6
  after_create :send_email
@@ -20,8 +21,14 @@ class UserInvitation < ActiveRecord::Base
20
21
  end
21
22
 
22
23
  def accept(user)
23
- Account.find(account_id).account_users << account_user = AccountUser.create(:user_id => user.id)
24
- account_user.set_roles(roles.split(',')) if roles
24
+ account = Account.find(account_id)
25
+ if account.account_users.where(:user_id => user.id).empty?
26
+ account.account_users << account_user = AccountUser.create(:user_id => user.id)
27
+ account_user.set_roles(roles.split(',')) if roles
28
+ true
29
+ else
30
+ false
31
+ end
25
32
  end
26
33
 
27
34
  private
@@ -37,5 +44,11 @@ class UserInvitation < ActiveRecord::Base
37
44
  def send_email
38
45
  IuguMailer.invitation(self).deliver
39
46
  end
47
+
48
+ def email_already_used?
49
+ if !AccountUser.joins(:user).where(:account_id => account_id, 'users.email' => email).empty?
50
+ errors.add(:email, "already used in this account")
51
+ end
52
+ end
40
53
  end
41
54
 
@@ -1,32 +1,57 @@
1
1
  %h2
2
2
  = I18n.t("iugu.account_domains") + " (##{@account.id})"
3
3
 
4
- %div
5
- = simple_form_for @account_domain, :url => create_domain_path(@account.id) do |f|
6
- = f.text_field :url
7
- = f.submit I18n.t("iugu.add")
8
- %br
9
- %div
10
- - @account_domains.each do |domain|
11
- %div
12
-
13
- = domain.url
14
-
15
- |
16
- - if domain.primary
17
- = I18n.t("iugu.primary")
18
- - else
19
- = link_to I18n.t("iugu.set_primary"), primary_domain_path(:account_id => @account.id, :domain_id => domain.id), :method => :post
20
-
21
- |
22
- - if domain.verified
23
- = I18n.t("iugu.verified")
24
- - else
25
- = link_to I18n.t("iugu.not_verified"), account_domains_instructions_path(:account_id => @account.id, :domain_id => domain.id)
26
-
27
- - if current_user.is?(:owner, @account)
4
+ - if IuguSDK::enable_subdomain == true
5
+ %div
6
+ = simple_form_for @account, :url => update_subdomain_path(:id => @account.id), :method => :put do |f|
7
+ - if @account.errors.any?
8
+ #error_explanation{ :style => "margin-bottom: 15px" }
9
+ %ul
10
+ - @account.errors.full_messages.each do |msg|
11
+ %li
12
+ = msg
13
+
14
+ = f.label :subdomain
15
+ = f.text_field :subdomain
16
+ = f.submit I18n.t("iugu.save")
17
+
18
+ - if IuguSDK::enable_custom_domain == true
19
+
20
+ %div
21
+ = simple_form_for @account_domain, :url => create_domain_path(@account.id) do |f|
22
+ - if @account_domain.errors.any?
23
+ #error_explanation{ :style => "margin-bottom: 15px" }
24
+ %ul
25
+ - @account_domain.errors.full_messages.each do |msg|
26
+ %li
27
+ = msg
28
+
29
+ = f.text_field :url
30
+ = f.submit I18n.t("iugu.add")
31
+ %br
32
+ %div
33
+ - @account_domains.each do |domain|
34
+ %div
35
+
36
+ = domain.url
37
+
38
+ - if domain.primary
39
+ |
40
+ = I18n.t("iugu.primary")
41
+ - else
42
+ - if domain.verified
43
+ |
44
+ = link_to I18n.t("iugu.set_primary"), primary_domain_path(:account_id => @account.id, :domain_id => domain.id), :method => :post
45
+
28
46
  |
29
- = link_to I18n.t("iugu.remove"), account_domains_destroy_path(:account_id => @account.id, :domain_id => domain.id), :method => :delete
47
+ - if domain.verified
48
+ = I18n.t("iugu.verified")
49
+ - else
50
+ = link_to I18n.t("iugu.not_verified"), account_domains_instructions_path(:account_id => @account.id, :domain_id => domain.id)
51
+
52
+ - if current_user.is?(:owner, @account)
53
+ |
54
+ = link_to I18n.t("iugu.remove"), account_domains_destroy_path(:account_id => @account.id, :domain_id => domain.id), :method => :delete
30
55
 
31
56
 
32
57
 
@@ -1,3 +1,6 @@
1
+ %div
2
+ Token:
3
+ = @domain.calculate_token
1
4
  %div
2
5
  = link_to I18n.t("iugu.verify_domain"), verify_domain_path(:account_id => @account.id, :domain_id => @domain.id), :method => :post
3
6
  %div
@@ -16,5 +16,7 @@
16
16
  = I18n.t("iugu.removing")
17
17
  - else
18
18
  = link_to I18n.t("iugu.undo"), account_users_cancel_destruction_path(:account_id => account_user.account_id, :user_id => account_user.user_id), :method => 'delete'
19
- %div
20
- = link_to I18n.t("iugu.invite"), new_invite_path(:account_id => @account.id)
19
+
20
+ - if current_user.is?(:owner, @account) || current_user.is?(:admin, @account)
21
+ %div
22
+ = link_to I18n.t("iugu.invite"), new_invite_path(:account_id => @account.id)
@@ -1,12 +1,26 @@
1
- = simple_form_for @user_invitation, :url => create_invite_path(:account_id => @account_id), :method => :post do |f|
1
+ = simple_form_for @user_invitation, :url => create_invite_path(@account.id), :method => :post do |f|
2
+ - if @user_invitation.errors.any?
3
+ #error_explanation{ :style => "margin-bottom: 15px" }
4
+ %ul
5
+ - @user_invitation.errors.full_messages.each do |msg|
6
+ %li
7
+ = msg
2
8
 
3
9
  %div
4
10
  = f.label :email
5
11
  = f.email_field :email
6
- - APP_ROLES['roles'].each do |role|
7
- %div
8
- = check_box_tag "user_invitation[roles][]", role, false, :id => "#{role}_checkbox"
9
- = label_tag "#{role}_checkbox", role
12
+ - if current_user.is?(:owner, @account)
13
+ - APP_ROLES['roles'].each do |role|
14
+ %div
15
+ = check_box_tag "user_invitation[roles][]", role, false, :id => "#{role}_checkbox"
16
+ = label_tag "#{role}_checkbox", role
17
+
18
+ - if current_user.is?(:admin, @account)
19
+ - APP_ROLES['roles'].each do |role|
20
+ - unless role == APP_ROLES['owner_role'] || role == APP_ROLES['admin_role']
21
+ %div
22
+ = check_box_tag "user_invitation[roles][]", role, false, :id => "#{role}_checkbox"
23
+ = label_tag "#{role}_checkbox", role
10
24
 
11
25
  %div
12
26
  = f.submit I18n.t("iugu.invite")
@@ -8,4 +8,7 @@
8
8
  = f.hidden_field :locale, :value => @matched_locale_from_browser
9
9
  .actions
10
10
  = f.button :submit, "Sign up"
11
- = render :partial => "devise/shared/links"
11
+
12
+ = link_to I18n.t("iugu.try_first"), try_first_path, :method => :post
13
+ %br
14
+ = render :partial => "iugu/shared/links"