From 4c0183630bda780eb58e93cb1f3a1ec7e9d67dcd Mon Sep 17 00:00:00 2001 From: Leonardo Merlin Date: Thu, 24 Sep 2015 17:17:22 -0300 Subject: [PATCH] proposal-box: vote with captch (initial commit) --- src/app/components/auth/auth.service.js | 26 ++++++++++++-------------- src/app/components/proposal-box/proposal-box.directive.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- src/app/components/proposal-box/proposal-box.html | 46 ++++++++++++++++++++++++++++++++++++++-------- src/app/components/proposal-box/proposal-box.scss | 8 ++++++++ src/app/components/proposal-grid/proposal-grid.html | 2 +- 5 files changed, 123 insertions(+), 30 deletions(-) diff --git a/src/app/components/auth/auth.service.js b/src/app/components/auth/auth.service.js index aa42270..bcb3ffc 100644 --- a/src/app/components/auth/auth.service.js +++ b/src/app/components/auth/auth.service.js @@ -53,11 +53,7 @@ } function activate (code) { - var url = '/api/v1/activate'; - // var data = { - // private_token: API.token, - // activation_code: code - // }; + var url = PATH.host + '/api/v1/activate'; var encodedData = 'private_token=' + API.token; encodedData += '&activation_code=' + code; @@ -77,12 +73,7 @@ } function changePassword (code, newPassword, newPasswordConfirmation){ - var url = '/api/v1/new_password'; - // var data = { - // code: code, - // password: newPassword, - // password_confirmation: newPasswordConfirmation - // }; + var url = PATH.host + '/api/v1/new_password'; var encodedData = 'code=' + code; encodedData += '&password=' + newPassword; encodedData += '&password_confirmation=' + newPasswordConfirmation; @@ -103,7 +94,7 @@ } function forgotPassword (data){ - var url = 'http://hom.login.dialoga.gov.br/api/v1/forgot_password'; + var url = PATH.host + '/api/v1/forgot_password'; var encodedData = ([ 'value=' + data.login, 'captcha_text=' + data.captcha_text, @@ -129,8 +120,7 @@ } function login (credentials) { - var hostProd = 'http://login.dialoga.gov.br'; - var url = hostProd + '/api/v1/login'; + var url = PATH.host + '/api/v1/login'; var encodedData = 'login=' + credentials.username + '&password=' + credentials.password; return $http @@ -149,6 +139,13 @@ }); } + function loginCaptcha (data) { + var url = PATH.host + '/api/v1/login-captcha'; + var encodedData = angular.element.param(data); + + return $http.post(url, encodedData); + } + function logout () { Session.destroy(); @@ -190,6 +187,7 @@ changePassword: changePassword, forgotPassword: forgotPassword, login: login, + loginCaptcha: loginCaptcha, logout: logout, isAuthenticated: isAuthenticated, isAuthorized: isAuthorized diff --git a/src/app/components/proposal-box/proposal-box.directive.js b/src/app/components/proposal-box/proposal-box.directive.js index 704e639..8e6e676 100644 --- a/src/app/components/proposal-box/proposal-box.directive.js +++ b/src/app/components/proposal-box/proposal-box.directive.js @@ -9,15 +9,19 @@ function proposalBox() { /** @ngInject */ - function ProposalBoxController($scope, $rootScope, $state, VOTE_STATUS, VOTE_OPTIONS, $log) { + function ProposalBoxController($scope, $rootScope, $state, $timeout, $interval, $window, VOTE_STATUS, VOTE_OPTIONS, AuthService, $log) { $log.debug('ProposalBoxController'); var vm = this; vm.$scope = $scope; vm.$rootScope = $rootScope; vm.$state = $state; + vm.$timeout = $timeout; + vm.$interval = $interval; + vm.$window = $window; vm.VOTE_STATUS = VOTE_STATUS; vm.VOTE_OPTIONS = VOTE_OPTIONS; + vm.AuthService = AuthService; vm.$log = $log; vm.init(); @@ -28,11 +32,11 @@ var vm = this; - vm.canVote = vm.canVote || false; + vm.showVote = vm.showVote || false; vm.focus = vm.focus || false; vm.STATE = null; vm.errorOnSkip = false; - vm.showAuthMessage = null; + vm.showCaptchaForm = null; vm.voteProposalRedirectURI = null; var slug = vm.topic.slug; @@ -68,6 +72,21 @@ vm.message = data.message; }); + + // Load captcha + var stop = null; + stop = vm.$interval(function(){ + var $el = angular.element('#serpro_captcha'); + + if ($el && $el.length > 0 ){ + vm.$window.initCaptcha($el[0]); + vm.$interval.cancel(stop); + stop = undefined; + }else{ + vm.$log.debug('captcha element not found.'); + } + + }, 10); }; ProposalBoxController.prototype.showContent = function (slug) { @@ -81,18 +100,56 @@ }); }; + ProposalBoxController.prototype.canVote = function () { + return false; + }; + + ProposalBoxController.prototype.submitCaptcha = function ($event, captchaForm) { + var vm = this; + + var target = $event.target; + var $target = angular.element(target); + var $captcha = $target.find('[name="txtToken_captcha_serpro_gov_br"]'); + + vm.sendingCaptcha = true; + vm.AuthService.loginCaptcha({ + captcha_text: captchaForm.captcha_text.$modelValue, + txtToken_captcha_serpro_gov_br: $captcha.val() + }).then(function(data) { + // SUCCESS + vm.$log.debug('register success.data', data); + + // get captcha_token + }, function(data) { + // ERROR + vm.$log.debug('register error.data', data); + + vm.sendingCaptchaError = {code: data.status }; + + if(data.status === 404){ + vm.$log.error('The api service is out!?'); + } + + }, function(data){ + // UPDATE + vm.$log.debug('register update.data', data); + }).finally(function(){ + vm.sendingCaptcha = false; + }); + }; + ProposalBoxController.prototype.vote = function (value) { var vm = this; - if(vm.$rootScope.currentUser){ + if(vm.canVote()){ vm.$scope.$emit('proposal-box:vote', { OPTION: value, proposal_id: vm.proposal.id }); vm.$log.debug('Sending vote', value); }else{ - vm.$log.info('Must be logged in...'); - vm.showAuthMessage = true; + vm.$log.debug('You cannot vote.'); + vm.showCaptchaForm = true; } }; @@ -135,7 +192,7 @@ proposal: '=', topic: '=', category: '=', - canVote: '=', + showVote: '=', focus: '@' // @ -> Text binding / one-way binding // = -> Direct model binding / two-way binding diff --git a/src/app/components/proposal-box/proposal-box.html b/src/app/components/proposal-box/proposal-box.html index 4c1d1aa..0bb7db3 100644 --- a/src/app/components/proposal-box/proposal-box.html +++ b/src/app/components/proposal-box/proposal-box.html @@ -65,18 +65,48 @@ -
+
-

Você precisa estar logado para votar na proposta

-
-

- Clique aqui para ir para a página de login -

+
+

Enviando captcha...

+
+
+ +
+
+ + +
+
+
+
+
+
+
Digite os caracteres acima:
+
+ + +
+
+
+ +
+
+ +
+
+
+
@@ -112,12 +142,12 @@
{{vm.proposal.abstract}}
-
+
-
+
diff --git a/src/app/components/proposal-box/proposal-box.scss b/src/app/components/proposal-box/proposal-box.scss index da182f1..1a780e9 100644 --- a/src/app/components/proposal-box/proposal-box.scss +++ b/src/app/components/proposal-box/proposal-box.scss @@ -91,9 +91,15 @@ .content { color: #262626; font-size: 24px; + font-size: 2.4rem; font-weight: bold; line-height: 24px; padding: 10px 30px; + + form { + font-size: 18px; + font-size: 1.8rem; + } } .message-icon { @@ -108,11 +114,13 @@ &--title { font-size: 22px; + font-size: 2.2rem; font-weight: bold; } &--message { font-size: 14px; + font-size: 1.4rem; font-weight: normal; line-height: 20px; margin-top: 48px; diff --git a/src/app/components/proposal-grid/proposal-grid.html b/src/app/components/proposal-grid/proposal-grid.html index fb9193c..18e3728 100644 --- a/src/app/components/proposal-grid/proposal-grid.html +++ b/src/app/components/proposal-grid/proposal-grid.html @@ -1,6 +1,6 @@
- +
-- libgit2 0.21.2