digital_heroes_startkit 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +37 -0
  4. data/Rakefile +36 -0
  5. data/app/assets/config/digital_heroes_startkit_manifest.js +0 -0
  6. data/app/assets/javascripts/authorization.coffee +79 -0
  7. data/app/assets/javascripts/avatar.coffee +16 -0
  8. data/app/assets/javascripts/datepicker.js +2 -0
  9. data/app/assets/javascripts/digital_heroes_startkit/cable.js +13 -0
  10. data/app/assets/javascripts/digital_heroes_startkit/channels/pills.coffee +23 -0
  11. data/app/assets/javascripts/digital_heroes_startkit/input.coffee +5 -0
  12. data/app/assets/javascripts/digital_heroes_startkit/pills.coffee +2 -0
  13. data/app/assets/javascripts/digital_heroes_startkit/snap.svg-min.js +21 -0
  14. data/app/assets/javascripts/digital_heroes_startkit.js +26 -0
  15. data/app/assets/javascripts/profile.coffee +53 -0
  16. data/app/assets/javascripts/profile_geodata.coffee +68 -0
  17. data/app/assets/javascripts/reg_fields_checkup.coffee +96 -0
  18. data/app/assets/javascripts/registration.coffee +35 -0
  19. data/app/assets/javascripts/resurrection.coffee +20 -0
  20. data/app/channels/application_cable/channel.rb +4 -0
  21. data/app/channels/application_cable/connection.rb +4 -0
  22. data/app/channels/pills_channel.rb +10 -0
  23. data/app/controllers/avatars_controller.rb +11 -0
  24. data/app/controllers/clients_controller.rb +62 -0
  25. data/app/controllers/email_access_cards_controller.rb +28 -0
  26. data/app/controllers/geodata_controller.rb +84 -0
  27. data/app/controllers/nickname_access_cards_controller.rb +30 -0
  28. data/app/controllers/oauth_controller.rb +178 -0
  29. data/app/controllers/openid_controller.rb +124 -0
  30. data/app/controllers/phone_access_cards_controller.rb +28 -0
  31. data/app/controllers/profile_controller.rb +114 -0
  32. data/app/controllers/response_controller.rb +17 -0
  33. data/app/controllers/start_kit_controller.rb +86 -0
  34. data/app/controllers/user_cards_controller.rb +305 -0
  35. data/app/jobs/log_analyzer_job.rb +50 -0
  36. data/app/jobs/password_resurrection_kill_job.rb +9 -0
  37. data/app/mailers/admin_mailer.rb +9 -0
  38. data/app/mailers/client_mailer.rb +3 -0
  39. data/app/mailers/kreator_mailer.rb +9 -0
  40. data/app/mailers/manager_mailer.rb +9 -0
  41. data/app/mailers/prometeus_mailer.rb +16 -0
  42. data/app/mailers/user_cards_mailer.rb +9 -0
  43. data/app/models/admin.rb +3 -0
  44. data/app/models/avatar.rb +23 -0
  45. data/app/models/city.rb +7 -0
  46. data/app/models/client.rb +31 -0
  47. data/app/models/country.rb +7 -0
  48. data/app/models/delayed_job.rb +20 -0
  49. data/app/models/email_access_card.rb +30 -0
  50. data/app/models/kreator.rb +3 -0
  51. data/app/models/log_error.rb +56 -0
  52. data/app/models/log_error_type.rb +3 -0
  53. data/app/models/manager.rb +3 -0
  54. data/app/models/nickname_access_card.rb +18 -0
  55. data/app/models/oauth_access_card.rb +38 -0
  56. data/app/models/phone_access_card.rb +19 -0
  57. data/app/models/prometeus.rb +50 -0
  58. data/app/models/user_card.rb +158 -0
  59. data/app/views/admin_mailer/greetings_email.html.erb +191 -0
  60. data/app/views/client_mailer/greetings_email.html.erb +191 -0
  61. data/app/views/kreator_mailer/greetings_email.html.erb +191 -0
  62. data/app/views/manager_mailer/greetings_email.html.erb +191 -0
  63. data/app/views/oauth/verify.slim +1 -0
  64. data/app/views/profile/_add_avatar.slim +12 -0
  65. data/app/views/profile/_user_profile.slim +95 -0
  66. data/app/views/profile/index.slim +12 -0
  67. data/app/views/user_cards/authorization.slim +72 -0
  68. data/app/views/user_cards/confirmation_but_access_code.slim +7 -0
  69. data/app/views/user_cards/confirmation_complete.slim +7 -0
  70. data/app/views/user_cards/registration.slim +86 -0
  71. data/app/views/user_cards/resurrection.slim +19 -0
  72. data/app/views/user_cards/resurrection_but_access_code.slim +7 -0
  73. data/app/views/user_cards/resurrection_no_access_code.slim +7 -0
  74. data/app/views/user_cards_mailer/email.html.erb +191 -0
  75. data/app/views/user_cards_mailer/resurrection.html.erb +191 -0
  76. data/config/initializers/session_manager.rb +2 -0
  77. data/config/routes.rb +53 -0
  78. data/lib/access_control.rb +3 -0
  79. data/lib/digital_heroes_startkit/engine.rb +25 -0
  80. data/lib/digital_heroes_startkit/version.rb +3 -0
  81. data/lib/digital_heroes_startkit.rb +5 -0
  82. data/lib/generators/digital_heroes_startkit/install_generator.rb +61 -0
  83. data/lib/generators/project/Capfile +36 -0
  84. data/lib/generators/project/Procfile +3 -0
  85. data/lib/generators/project/app/assets/stylesheets/application.scss +16 -0
  86. data/lib/generators/project/app/assets/stylesheets/base/_auth.scss +37 -0
  87. data/lib/generators/project/app/assets/stylesheets/base/_buttons.scss +62 -0
  88. data/lib/generators/project/app/assets/stylesheets/base/_checkbox.scss +71 -0
  89. data/lib/generators/project/app/assets/stylesheets/base/_datepicker.scss +620 -0
  90. data/lib/generators/project/app/assets/stylesheets/base/_form.scss +81 -0
  91. data/lib/generators/project/app/assets/stylesheets/base/_notice.scss +14 -0
  92. data/lib/generators/project/app/assets/stylesheets/base/_pills.scss +39 -0
  93. data/lib/generators/project/app/assets/stylesheets/base/_profile.scss +67 -0
  94. data/lib/generators/project/app/assets/stylesheets/base/_reset.scss +49 -0
  95. data/lib/generators/project/app/assets/stylesheets/base/_social.scss +52 -0
  96. data/lib/generators/project/app/assets/stylesheets/base/_user.scss +0 -0
  97. data/lib/generators/project/app/assets/stylesheets/base/_user_avatar.scss +18 -0
  98. data/lib/generators/project/app/assets/stylesheets/base/styles.scss.txt +35 -0
  99. data/lib/generators/project/app/assets/stylesheets/plugins/sumoselect.scss +296 -0
  100. data/lib/generators/project/app/controllers/application_controller.rb +3 -0
  101. data/lib/generators/project/app/views/layouts/_counts.html.erb +29 -0
  102. data/lib/generators/project/app/views/layouts/_footer.slim +6 -0
  103. data/lib/generators/project/app/views/layouts/_head.slim +7 -0
  104. data/lib/generators/project/app/views/layouts/_header.slim +47 -0
  105. data/lib/generators/project/app/views/layouts/_mobile_menu.slim +11 -0
  106. data/lib/generators/project/app/views/layouts/application.slim +26 -0
  107. data/lib/generators/project/app/views/layouts/mailer.html.erb +13 -0
  108. data/lib/generators/project/app/views/layouts/mailer.text.erb +1 -0
  109. data/lib/generators/project/config/application.access.yml +66 -0
  110. data/lib/generators/project/config/application.db.yml +5 -0
  111. data/lib/generators/project/config/application.google.yml +3 -0
  112. data/lib/generators/project/config/application.oauth.yml +135 -0
  113. data/lib/generators/project/config/application.robokassa.yml +10 -0
  114. data/lib/generators/project/config/application.sendgrid.yml +3 -0
  115. data/lib/generators/project/config/application.telegram.yml +22 -0
  116. data/lib/generators/project/config/application.yml +28 -0
  117. data/lib/generators/project/config/cable.yml +6 -0
  118. data/lib/generators/project/config/capistrano.yml +78 -0
  119. data/lib/generators/project/config/database.yml +52 -0
  120. data/lib/generators/project/config/deploy/application.secrets.yml +4 -0
  121. data/lib/generators/project/config/deploy/domain.crt +92 -0
  122. data/lib/generators/project/config/deploy/domain.key +51 -0
  123. data/lib/generators/project/config/deploy/nginx.conf +50 -0
  124. data/lib/generators/project/config/deploy/production.rb +67 -0
  125. data/lib/generators/project/config/deploy/secrets.yml +32 -0
  126. data/lib/generators/project/config/deploy/secrets.yml.enc +1 -0
  127. data/lib/generators/project/config/deploy/secrets.yml.key +1 -0
  128. data/lib/generators/project/config/deploy/staging.rb +61 -0
  129. data/lib/generators/project/config/deploy/virgin_conf/database.yml +42 -0
  130. data/lib/generators/project/config/deploy/virgin_conf/nginx_site.conf +47 -0
  131. data/lib/generators/project/config/deploy/virgin_conf/nginx_site_ssl.conf +57 -0
  132. data/lib/generators/project/config/deploy/virgin_conf/postfix.cf +685 -0
  133. data/lib/generators/project/config/deploy/virgin_conf/production.rb +67 -0
  134. data/lib/generators/project/config/deploy/virgin_conf/puma.rb +66 -0
  135. data/lib/generators/project/config/deploy/virgin_sh/capistrano.sh +18 -0
  136. data/lib/generators/project/config/deploy/virgin_sh/foreman.sh +19 -0
  137. data/lib/generators/project/config/deploy/virgin_sh/rvm_install.sh +10 -0
  138. data/lib/generators/project/config/deploy.rb +377 -0
  139. data/lib/generators/project/config/environments/development.rb +91 -0
  140. data/lib/generators/project/config/environments/production.rb +142 -0
  141. data/lib/generators/project/config/initializers/session_store.rb +3 -0
  142. data/lib/generators/project/config/locales/ru.yml +444 -0
  143. data/lib/generators/project/db/migrate/10_create_user_cards.rb +19 -0
  144. data/lib/generators/project/db/migrate/12_create_email_access_cards.rb +15 -0
  145. data/lib/generators/project/db/migrate/13_create_nickname_access_cards.rb +15 -0
  146. data/lib/generators/project/db/migrate/14_create_phone_access_cards.rb +16 -0
  147. data/lib/generators/project/db/migrate/15_create_oauth_access_cards.rb +19 -0
  148. data/lib/generators/project/db/migrate/1_create_kreators.rb +9 -0
  149. data/lib/generators/project/db/migrate/21_create_avatars.rb +12 -0
  150. data/lib/generators/project/db/migrate/30_create_countries.rb +12 -0
  151. data/lib/generators/project/db/migrate/32_create_cities.rb +12 -0
  152. data/lib/generators/project/db/migrate/3_create_admins.rb +9 -0
  153. data/lib/generators/project/db/migrate/44_add_sessions_table.rb +12 -0
  154. data/lib/generators/project/db/migrate/4_create_managers.rb +10 -0
  155. data/lib/generators/project/db/migrate/5_create_clients.rb +17 -0
  156. data/lib/generators/project/db/migrate/60_create_log_error_types.rb +9 -0
  157. data/lib/generators/project/db/migrate/61_create_log_errors.rb +12 -0
  158. data/lib/generators/project/db/migrate/78_create_delayed_jobs.rb +22 -0
  159. data/lib/generators/project/db/seeds.rb +27 -0
  160. data/lib/generators/project/geodata/cities.csv +25305 -0
  161. data/lib/generators/project/geodata/countries.csv +250 -0
  162. data/lib/generators/project/public/401.html +75 -0
  163. data/lib/generators/project/public/404.html +67 -0
  164. data/lib/generators/project/public/422.html +67 -0
  165. data/lib/generators/project/public/500.html +66 -0
  166. data/lib/generators/project/public/_receiver.html +10 -0
  167. data/lib/generators/project/public/apple-touch-icon-precomposed.png +0 -0
  168. data/lib/generators/project/public/apple-touch-icon.png +0 -0
  169. data/lib/generators/project/public/favicon.ico +0 -0
  170. data/lib/generators/project/public/robots.txt +1 -0
  171. data/lib/happy_birthday.rb +5 -0
  172. data/lib/link.rb +16 -0
  173. data/lib/oauth/oauth.rb +72 -0
  174. data/lib/oauth/oauth_fb.rb +48 -0
  175. data/lib/oauth/oauth_gg.rb +50 -0
  176. data/lib/oauth/oauth_im.rb +51 -0
  177. data/lib/oauth/oauth_mr.rb +53 -0
  178. data/lib/oauth/oauth_vk.rb +3 -0
  179. data/lib/oauth/oauth_yx.rb +51 -0
  180. data/lib/openid/openid.rb +46 -0
  181. data/lib/openid/openid_st.rb +2 -0
  182. data/lib/phone_cutter.rb +9 -0
  183. data/lib/response.rb +51 -0
  184. data/lib/session_manager.rb +47 -0
  185. data/lib/sms_lib.rb +30 -0
  186. data/lib/social_networks.rb +328 -0
  187. data/lib/tasks/digital_heroes_startkit_tasks.rake +83 -0
  188. data/lib/telegram.rb +33 -0
  189. metadata +497 -0
@@ -0,0 +1,178 @@
1
+ class OauthController < ResponseController
2
+
3
+ before_action :set_state, only: :authorize
4
+ before_action :check_state, only: [:oauth_verify, :openid_verify]
5
+
6
+ def authorize
7
+ if params[:name].present? && params[:type].present?
8
+ set_oauth params[:name], params[:type]
9
+ else
10
+ ActiveRecord::Base.logger.error('имя и тип соц.сети не пришли')
11
+ redirect_to authorization_url, flash: {pills: 'имя и тип соц.сети не пришли'}
12
+ end
13
+
14
+ # отправка пользователя на подтверждение
15
+ redirect_to @oauth.auth_request
16
+ end
17
+
18
+ def oauth_verify
19
+ if params[:code].present?
20
+ # обмен code на токен
21
+ access_card_params = @oauth.auth_request :step2, {:code => params[:code]}
22
+
23
+ case access_card_params[:state]
24
+ when :ok
25
+ sign_in access_card_params
26
+ redirect_to profile_path
27
+ else
28
+ # данные не обработаны по причине access_card_params[:error]
29
+ ActiveRecord::Base.logger.error("данные не обработаны по причине #{access_card_params[:error]}")
30
+ # ActionCable.server.broadcast "pills", {state: 'red', message: "данные не обработаны по причине #{access_card_params[:error]}", url: authorization_url}
31
+ redirect_to authorization_url, flash: {pills: "данные не обработаны по причине #{access_card_params[:error]}"}
32
+ end
33
+ else
34
+ #code не пришёл от api соц.сети
35
+ ActiveRecord::Base.logger.error("code не пришёл от api соц.сети")
36
+ redirect_to authorization_url, flash: {pills: 'code не пришёл от api соц.сети'}
37
+ end
38
+ end
39
+
40
+ def openid_verify
41
+ if params['openid.claimed_id'].present?
42
+ access_card_params = @oauth.oauth_access_card_params /.*\/(\d+)$/.match(params['openid.claimed_id'])[1]
43
+
44
+ case access_card_params[:state]
45
+ when :ok
46
+ sign_in access_card_params
47
+ redirect_to profile_path
48
+ else
49
+ # данные не обработаны по причине access_card_params[:error]
50
+ ActiveRecord::Base.logger.error("данные не обработаны по причине #{access_card_params[:error]}")
51
+ # ActionCable.server.broadcast "pills", {state: 'red', message: "данные не обработаны по причине #{access_card_params[:error]}", url: authorization_url}
52
+ redirect_to authorization_url, flash: {pills: "данные не обработаны по причине #{access_card_params[:error]}"}
53
+ end
54
+ else
55
+ #code не пришёл от api соц.сети
56
+ ActiveRecord::Base.logger.error("openid.claimed_id не пришёл от api соц.сети")
57
+ redirect_to authorization_url, flash: {pills: 'openid.claimed_id не пришёл от api соц.сети'}
58
+
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ def set_state
65
+ key = SecureRandom.hex(16)
66
+ session[:oauth_state] = {key => {name: params[:name], type: params[:type]}}
67
+ end
68
+
69
+ # проверка пришедшего state на соответствие отправленному
70
+ def check_state
71
+ state_key = nil
72
+
73
+ if params[:state].present?
74
+ state_key = params[:state]
75
+ elsif params['openid.state']
76
+ state_key = params['openid.state']
77
+ end
78
+
79
+ unless state_key.nil?
80
+ if session[:oauth_state] && session[:oauth_state].keys.first == state_key
81
+ state_value = session[:oauth_state].values.first
82
+ set_oauth state_value[:name], state_value[:type]
83
+ else
84
+ ActiveRecord::Base.logger.error("state #{state_key} подделан")
85
+ redirect_to authorization_url, flash: {pills: "state #{state_key} подделан"}
86
+ end
87
+ else
88
+ ActiveRecord::Base.logger.error('state не пришёл от api соц.сети')
89
+ redirect_to authorization_url, flash: {pills: 'state не пришёл от api соц.сети'}
90
+ end
91
+
92
+ end
93
+
94
+ def set_oauth name, type
95
+ classname = "#{type}_#{name}"
96
+ require "#{type}/#{type}"
97
+ require "#{type}/#{classname}"
98
+
99
+ @oauth = classname.classify.constantize.new eval("#{type}_verify_url"), session[:oauth_state]
100
+ end
101
+
102
+ def sign_in access_card_params
103
+ # есть ли у нас такая скрепа
104
+ access_card = OauthAccessCard.find_by({oauth_name: access_card_params[:oauth_name], oauth_uid: [:oauth_uid]})
105
+ # уже авторизован и решил прикрепить скрепу!
106
+ if @current_user_card
107
+ # если ли она среди карточек именно этого юзера
108
+ user_access_card = @current_user_card.oauth_access_cards.find_by({oauth_name: access_card_params[:oauth_name], oauth_uid: [:oauth_uid]})
109
+ if user_access_card.nil?
110
+ if access_card
111
+ # переезд к другому юзеру
112
+ u = access_card.user_card
113
+ access_card.update(user_id: @current_user_card.id)
114
+ user_access_card = access_card
115
+ # если забрал последнюю скрепу и нет больше карточек доступа - дезинтегрируем юзера
116
+ if u.access_cards_count == 0
117
+ u.userable.destroy
118
+ else
119
+ u.set_active_card
120
+ end
121
+ else
122
+ # прикрепил
123
+ user_access_card = @current_user_card.oauth_access_cards.create access_card_params
124
+ end
125
+ else
126
+ # просто заапдейтил токен и аву
127
+ user_access_card.update(access_token: access_card_params[:access_token], token_expired: access_card_params[:token_expired], photourl: access_card_params[:photourl])
128
+ end
129
+
130
+ profile_update
131
+
132
+ else
133
+ # привет старина!
134
+ if access_card
135
+ user_card = access_card.user_card
136
+ # обновляем токены, аву, авторизуем
137
+ access_card.update(access_token: access_card_params[:access_token], token_expired: access_card_params[:token_expired], photourl: access_card_params[:photourl])
138
+ # social_network_api :update_user_profile, sign_as, access_card # дополнение профиля пользователя данными из соц сети
139
+ user_card.authorize! access_card
140
+ current_user
141
+
142
+ # привет новичок! добро пожаловать на регистрацию!
143
+ else
144
+ # создаём клиента
145
+ client = Client.create
146
+ # и его карточку доступа
147
+ # остальные карточки доступа он заполняет в профиле
148
+ #
149
+ # здесь как вариант поиск юзера и сопоставление по мылу, прикрепление к нему, но это уже совсем другая история
150
+ #
151
+ access_card = client.user_card.oauth_access_cards.create(
152
+ {
153
+ oauth_name: access_card_params[:oauth_name],
154
+ oauth_uid: access_card_params[:oauth_uid],
155
+ access_token: access_card_params[:access_token],
156
+ token_expired: access_card_params[:token_expired],
157
+ photourl: access_card_params[:photourl]
158
+ }
159
+ )
160
+
161
+ client.user_card.update({password: SecureRandom.hex(8)})
162
+
163
+ client.user_card.authorize! access_card
164
+ current_user
165
+ profile_update
166
+
167
+ end
168
+
169
+ end
170
+
171
+ end
172
+
173
+ # дополнение профиля пользователя данными из соц сети
174
+ def profile_update
175
+
176
+ end
177
+
178
+ end
@@ -0,0 +1,124 @@
1
+ class OpenidController < ResponseController
2
+
3
+ before_action :set_oauth, only: [:authorize, :verify]
4
+
5
+ def authorize
6
+ # отправка пользователя на подтверждение
7
+ redirect_to @oauth.auth_request
8
+ end
9
+
10
+ def receiver
11
+ if params[:code].present?
12
+ # обмен code на токен
13
+ access_card_params = @oauth.auth_request :step2, {:code => params[:code]}
14
+
15
+ case access_card_params[:state]
16
+ when :ok
17
+ sign_in access_card_params
18
+ redirect_to profile_path
19
+ else
20
+ # данные не обработаны по причине access_card_params[:error]
21
+ ActiveRecord::Base.logger.error("данные не обработаны по причине #{access_card_params[:error]}")
22
+ # ActionCable.server.broadcast "pills", {state: 'red', message: "данные не обработаны по причине #{access_card_params[:error]}", url: authorization_url}
23
+ redirect_to authorization_url, flash: {pills: "данные не обработаны по причине #{access_card_params[:error]}"}
24
+ end
25
+ else
26
+ #code не пришёл от api соц.сети
27
+ ActiveRecord::Base.logger.error("code не пришёл от api соц.сети")
28
+ # ActionCable.server.broadcast "pills", {state: 'red', message: 'code не пришёл от api соц.сети', url: authorization_url}
29
+ redirect_to authorization_url, flash: {pills: 'code не пришёл от api соц.сети'}
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def set_oauth
36
+ if params[:state].present?
37
+ classname = "openid_#{params[:state]}"
38
+ require "openid/openid"
39
+ require "openid/#{classname}"
40
+
41
+ @oauth = classname.classify.constantize.new oauth_verify_url, params[:state].to_sym
42
+ else
43
+ ActiveRecord::Base.logger.error('state не пришёл от api соц.сети')
44
+ # ActionCable.server.broadcast "pills", {state: 'red', message: 'state не пришёл от api соц.сети', url: authorization_url}
45
+ redirect_to authorization_url, flash: {pills: 'state не пришёл от api соц.сети'}
46
+ end
47
+ end
48
+
49
+ def sign_in access_card_params
50
+ # есть ли у нас такая скрепа
51
+ access_card = OauthAccessCard.find_by({oauth_name: access_card_params[:oauth_name], oauth_uid: [:oauth_uid]})
52
+ # уже авторизован и решил прикрепить скрепу!
53
+ if @current_user_card
54
+ # если ли она среди карточек именно этого юзера
55
+ user_access_card = @current_user_card.oauth_access_cards.find_by({oauth_name: access_card_params[:oauth_name], oauth_uid: [:oauth_uid]})
56
+ if user_access_card.nil?
57
+ if access_card
58
+ # спизданул у другого юзера
59
+ u = access_card.user_card
60
+ access_card.update(user_id: @current_user_card.id)
61
+ user_access_card = access_card
62
+ # если забрал последнюю скрепу и нет больше карточек доступа - дезинтегрируем юзера
63
+ if u.access_cards_count == 0
64
+ u.userable.destroy
65
+ else
66
+ u.set_active_card
67
+ end
68
+ else
69
+ # прикрепил
70
+ user_access_card = @current_user_card.oauth_access_cards.create access_card_params
71
+ end
72
+ else
73
+ # просто заапдейтил токен
74
+ user_access_card.update(access_token: access_card_params[:access_token], token_expired: access_card_params[:token_expired])
75
+ end
76
+
77
+ profile_update
78
+
79
+ else
80
+ # привет старина!
81
+ if access_card
82
+ user_card = access_card.user_card
83
+ # обновляем токены, авторизуем
84
+ access_card.update(access_token: access_card_params[:access_token], token_expired: access_card_params[:token_expired])
85
+ # social_network_api :update_user_profile, sign_as, access_card # дополнение профиля пользователя данными из соц сети
86
+ user_card.authorize! access_card
87
+ current_user
88
+
89
+ # новичок! привет ссанина! добро пожаловать на регистрацию!
90
+ else
91
+ # создаём клиента
92
+ client = Client.create
93
+ # и его карточку доступа
94
+ # остальные карточки доступа он заполняет в профиле
95
+ #
96
+ # здесь как вариант поиск юзера и сопоставление по мылу, прикрепление к нему, но это уже совсем другая история
97
+ #
98
+ access_card = client.user_card.oauth_access_cards.create(
99
+ {
100
+ oauth_name: access_card_params[:oauth_name],
101
+ oauth_uid: access_card_params[:oauth_uid],
102
+ access_token: access_card_params[:access_token],
103
+ token_expired: access_card_params[:token_expired]
104
+ }
105
+ )
106
+
107
+ client.user_card.update({password: SecureRandom.hex(8)})
108
+
109
+ client.user_card.authorize! access_card
110
+ current_user
111
+ profile_update
112
+
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ # дополнение профиля пользователя данными из соц сети
120
+ def profile_update
121
+
122
+ end
123
+
124
+ end
@@ -0,0 +1,28 @@
1
+ class PhoneAccessCardsController < ApplicationController
2
+ layout false, only: [:create, :update]
3
+ before_action :set_access_card, only: [:update, :destroy]
4
+
5
+ def create
6
+ @access_card = PhoneAccessCard.new(access_card_params)
7
+ if @access_card.save
8
+ @result = :ok
9
+ else
10
+ @result = :fail
11
+ end
12
+ end
13
+
14
+ def destroy
15
+ @access_card.destroy
16
+ end
17
+
18
+ private
19
+ # Use callbacks to share common setup or constraints between actions.
20
+ def set_access_card
21
+ @access_card = PhoneAccessCard.find(params[:id])
22
+ end
23
+
24
+ # Never trust parameters from the scary internet, only allow the white list through.
25
+ def access_card_params
26
+ params.require(:phone_access_card).permit(:user_id, :value, :active)
27
+ end
28
+ end
@@ -0,0 +1,114 @@
1
+ class ProfileController < ResponseController
2
+
3
+ before_action :prms, only: :update
4
+
5
+ def index
6
+ if @current_user_card
7
+ @type = @current_user_card.whois
8
+ @name = @current_user.name
9
+ @avatar = @current_user.avatar
10
+
11
+ if @current_user_card.client?
12
+ @sex_all = Client.sex.values.map{ |sex|
13
+ {id: sex, name: I18n.t(sex)}
14
+ }
15
+ @sex = @current_user.sex
16
+ @birthday = @current_user.birthday ? @current_user.birthday.strftime("%d.%m.%Y") : nil
17
+ @birthday_default_view = @birthday.nil? ? (DateTime.now - 30.years).strftime("%d.%m.%Y") : @birthday
18
+ @country = @current_user.country
19
+ @city = @current_user.city
20
+ end
21
+
22
+ @access_cards = [
23
+ {cards: @current_user_card.nickname_access_cards, classname: 'NicknameAccessCard', by: 'nickname'},
24
+ {cards: @current_user_card.email_access_cards, classname: 'EmailAccessCard', by: 'email'},
25
+ {cards: @current_user_card.phone_access_cards, classname: 'PhoneAccessCard', by: 'phone'}
26
+ ]
27
+
28
+ @access_cards.each do |cards_pack|
29
+ count = cards_pack[:classname].constantize.where(user_card_id: @current_user_card.id).count
30
+ cards_pack[:add] = count > 0 && count < CONFIG[:access_cards][cards_pack[:by].to_sym][:quantity] ? true : false
31
+ end
32
+
33
+ @ressurection_action = session[:ressurection_action]
34
+ session[:ressurection_action]= nil
35
+ end
36
+ end
37
+
38
+ def update
39
+
40
+ if @current_user_card
41
+ begin
42
+ # параметры юзера
43
+ @current_user.update(get_user_params)
44
+ @current_user.errors.messages.each do |message|
45
+ @response.add_detail message[0]
46
+ end
47
+ # параметры юзеркарты
48
+ @response.add_detail 'password' unless @current_user_card.update(password: @user_card_params[:password])
49
+
50
+ CONFIG[:access_cards].keys.each do |access_card_type|
51
+ @user_card_params[access_card_type].each do |access_card|
52
+ case access_card['id']
53
+ when 0
54
+ # создание новой карты
55
+ @response.add_detail access_card_type.to_s unless eval "@current_user_card.#{access_card_type}_access_cards.create(value: access_card['value'])"
56
+ else
57
+ # редактирование имеющейся
58
+ @response.add_detail access_card_type.to_s unless "@current_user_card.#{access_card_type}_access_cards[access_card['id']].update(value: access_card['value'])"
59
+ end
60
+ end
61
+ end
62
+
63
+ rescue Exception => error
64
+ @response.error_state error.message
65
+ end
66
+ else
67
+ @response.set_state 'shit'
68
+ @response.add_detail 'пользователь не найден'
69
+ @response.set_url root_path
70
+ end
71
+
72
+ send_response
73
+
74
+ end
75
+
76
+ def kill_avatar
77
+
78
+ begin
79
+ @current_user.avatar.destroy if @current_user.avatar.present?
80
+ @response.set_state 'ok'
81
+ rescue Exception => error
82
+ @response.error_state error.message
83
+ end
84
+
85
+ send_response
86
+
87
+ end
88
+
89
+ def add_avatar
90
+ begin
91
+ @current_user.avatar.destroy unless @current_user.avatar.nil?
92
+ picture = @current_user.create_avatar({uploaded_file: params.require(:avatar).permit(:uploaded_file)[:uploaded_file]})
93
+ @response.set_state 'ok'
94
+ @response.add_var ({picture: {id: picture.id, src: picture.uploaded_file.url(:preview)}})
95
+
96
+ rescue Exception => error
97
+ @response.error_state error.message
98
+ end
99
+
100
+ send_response
101
+ end
102
+
103
+ private
104
+
105
+ def get_user_params
106
+ params.require(:user).permit(:name, :sex, :birthday)
107
+ end
108
+
109
+ def prms
110
+ require 'active_support/core_ext/hash'
111
+ @user_card_params = JSON.parse(params.require(:user_card)).with_indifferent_access
112
+ end
113
+
114
+ end
@@ -0,0 +1,17 @@
1
+ class ResponseController < ApplicationController
2
+
3
+ before_action :set_response_env
4
+
5
+ private
6
+
7
+ def set_response_env
8
+ require 'response'
9
+
10
+ @response = Response.new
11
+ end
12
+
13
+ def send_response
14
+ render json: @response.get
15
+ end
16
+
17
+ end
@@ -0,0 +1,86 @@
1
+ class StartKitController < ActionController::Base
2
+
3
+ protect_from_forgery with: :exception
4
+
5
+ before_action :charge_session_manager, :access_level_control
6
+
7
+ def charge_session_manager
8
+ $session_manager.set_session session
9
+ $session_manager.set_cookies cookies
10
+ end
11
+
12
+ # уровень доступа
13
+ def access_level_control
14
+ current_user
15
+
16
+ if CONFIG[:access_levels][controller_name.to_sym].present?
17
+ access_node = CONFIG[:access_levels][controller_name.to_sym].select{|level, actions| actions.include?(action_name)} # концентрация мозга и волшебство Ruby, это прекрасно
18
+ if access_node.any?
19
+ access_granted = @current_user_card && CONFIG[:access_aliases][@access_level] >= CONFIG[:access_aliases][access_node.keys.first] # сюда вложено многое при Идеальной структуре конфигурационнного YAML!
20
+ unless check_access_level(:admin)
21
+ if access_granted && /edit/ =~ action_name # доп проверка на случай лайфхакеров желающих заняться редактированием не своих объектов, допускается редактирование всеми начиная с редактора
22
+ # с проверкой на friendly id
23
+ object = ActiveRecord::Base.connection.column_exists?(controller_name, :slug) ? controller_name.classify.constantize.friendly.find(params[:id]) : controller_name.classify.constantize.find(params[:id])
24
+ access_granted = edit_checkout object
25
+ end
26
+ end
27
+
28
+ redirect_to "/401.html" unless access_granted
29
+ end
30
+ end
31
+ end
32
+
33
+ def current_user
34
+ unless session[:uid].nil?
35
+ @current_user_card = UserCard.find_by(id: session[:uid])
36
+ @access_level = @current_user_card.access_level.to_sym
37
+ else
38
+ @current_user_card = nil
39
+ end
40
+
41
+ @current_user = @current_user_card.nil? ? nil : @current_user_card.userable
42
+ @active_card = @current_user_card.my_active_card unless @current_user.nil?
43
+ end
44
+
45
+ helper_method :current_user
46
+
47
+
48
+ # def cookie_authorize
49
+ # user = UserCard.find_by(id: cookies[:uid])
50
+ # if user.present? && cookies[:chk] == Digest::MD5.new.update("#{user.id}#{'pussy'}#{user.password_digest}")
51
+ # user.authenticate(user.password)
52
+ # session[:uid] = user.id
53
+ # user
54
+ # else
55
+ # nil
56
+ # end
57
+ # end
58
+
59
+ def edit_checkout object
60
+ begin
61
+ master = object.my_master
62
+ rescue
63
+ master = nil
64
+ end
65
+
66
+ if (check_access_level CONFIG[:access_edit_bottom].to_sym)
67
+ access_granted = true
68
+ else
69
+ if master && @current_user
70
+ access_granted = (master.id == @current_user.id)
71
+ end
72
+ end
73
+
74
+ access_granted
75
+ end
76
+
77
+ helper_method :edit_checkout
78
+
79
+ # запрос достиг ли юзер уровня доступа алиаса
80
+ def check_access_level _alias
81
+ @current_user_card && CONFIG[:access_aliases][@access_level] >= CONFIG[:access_aliases][_alias] # сюда вложено многое при Идеальной структуре конфигурационнного YAML!
82
+ end
83
+
84
+ helper_method :check_access_level
85
+
86
+ end