Commit d7e71522d9cf5bd2fe2c666da2071b234a1e79cb

Authored by Leonardo Merlin
2 parents a41b9957 b635ef34

Merge branch 'login'

Conflicts:
	src/app/partials/login/login.scss
gulp/build.js
... ... @@ -68,7 +68,7 @@ gulp.task('html', ['inject', 'partials'], function () {
68 68 // Only applies for fonts from bower dependencies
69 69 // Custom fonts are handled by the "other" task
70 70 gulp.task('fonts', function () {
71   - return gulp.src($.mainBowerFiles())
  71 + return gulp.src($.mainBowerFiles().concat('bower_components/bootstrap-sass-official/assets/fonts/bootstrap/*'))
72 72 .pipe($.filter('**/*.{eot,svg,ttf,woff,woff2}'))
73 73 .pipe($.flatten())
74 74 .pipe(gulp.dest(path.join(conf.paths.dist, '/fonts/')));
... ...
src/app/components/auth/auth.service.js 0 → 100644
... ... @@ -0,0 +1,135 @@
  1 +(function() {
  2 + 'use strict';
  3 +
  4 + angular
  5 + .module('dialoga')
  6 + .factory('Session', Session)
  7 + .factory('AuthService', AuthService)
  8 + .factory('AuthInterceptor', AuthInterceptor);
  9 +
  10 + /** @ngInject */
  11 + function AuthService($http, $rootScope, Session, AUTH_EVENTS, api, $log) {
  12 +
  13 + var service = {
  14 + login: login,
  15 + logout: logout,
  16 + isAuthenticated: isAuthenticated,
  17 + isAuthorized: isAuthorized
  18 + };
  19 +
  20 + $log.debug('AuthService', service);
  21 + return service;
  22 +
  23 + function login (credentials) {
  24 + var url = api.host + 'login';
  25 + var encodedData = 'login=' + credentials.username + '&password=' + credentials.password;
  26 +
  27 + return $http
  28 + .post(url, encodedData)
  29 + .then(function(response) {
  30 + $log.debug('AuthService.login [SUCCESS] response', response);
  31 +
  32 + var currentUser = Session.create(response.data);
  33 +
  34 + $rootScope.$broadcast(AUTH_EVENTS.loginSuccess, currentUser);
  35 + return currentUser;
  36 + }, function(response) {
  37 + $log.debug('AuthService.login [FAIL] response', response);
  38 + $rootScope.$broadcast(AUTH_EVENTS.loginFailed);
  39 + });
  40 + }
  41 +
  42 + function logout () {
  43 + Session.destroy();
  44 + }
  45 +
  46 + function isAuthenticated () {
  47 + return !!Session.userId;
  48 + }
  49 +
  50 + function isAuthorized (authorizedRoles) {
  51 + if (!angular.isArray(authorizedRoles)) {
  52 + authorizedRoles = [authorizedRoles];
  53 + }
  54 +
  55 + return (service.isAuthenticated() && authorizedRoles.indexOf(Session.userRole) !== -1);
  56 + }
  57 + }
  58 +
  59 + /** @ngInject */
  60 + function Session($cookies, $log) {
  61 +
  62 + var service = {};
  63 +
  64 + var currentUser = $cookies.getObject('currentUser') || {};
  65 +
  66 + service.create = function(data) {
  67 +
  68 + currentUser.id = data.id;
  69 + currentUser.email = data.email;
  70 + currentUser.login = data.login;
  71 + currentUser.permissions = data.permissions;
  72 + currentUser.person = data.person;
  73 + currentUser.private_token = data.private_token;
  74 + currentUser.activated = data.activated;
  75 +
  76 + $cookies.putObject('currentUser', currentUser);
  77 +
  78 + $log.debug('User session created.', currentUser);
  79 + return currentUser;
  80 + };
  81 +
  82 + service.destroy = function() {
  83 +
  84 + currentUser = {};
  85 +
  86 + $cookies.remove('currentUser');
  87 +
  88 + $log.debug('User session destroyed.');
  89 + };
  90 +
  91 + service.getCurrentUser = function () {
  92 + return currentUser;
  93 + };
  94 +
  95 + return service;
  96 + }
  97 +
  98 + /** @ngInject */
  99 + function AuthInterceptor ($rootScope, $q, AUTH_EVENTS) {
  100 + return {
  101 + responseError: function(response) {
  102 + $rootScope.$broadcast({
  103 + 401: AUTH_EVENTS.notAuthenticated,
  104 + 403: AUTH_EVENTS.notAuthorized,
  105 + 419: AUTH_EVENTS.sessionTimeout,
  106 + 440: AUTH_EVENTS.sessionTimeout
  107 + }[response.status], response);
  108 + return $q.reject(response);
  109 + }
  110 + };
  111 + }
  112 +
  113 + /** @ngInject */
  114 + function AuthResolver($q, $rootScope, $state){
  115 + return {
  116 + resolve: function () {
  117 + var deferred = $q.defer();
  118 + var unwatch = $rootScope.$watch('currentUser', function (currentUser) {
  119 + if (angular.isDefined(currentUser)) {
  120 + if (currentUser) {
  121 + deferred.resolve(currentUser);
  122 + } else {
  123 + deferred.reject();
  124 + // TODO: too many responsibilities?
  125 + $state.go('login');
  126 + }
  127 + unwatch();
  128 + }
  129 + });
  130 + return deferred.promise;
  131 + }
  132 + };
  133 + }
  134 +
  135 +})();
... ...
src/app/index.config.js
... ... @@ -3,14 +3,38 @@
3 3  
4 4 angular
5 5 .module('dialoga')
  6 + .config(configAuthInterceptor)
6 7 .config(config);
7 8  
8 9 /** @ngInject */
  10 + function configAuthInterceptor ($httpProvider){
  11 +
  12 + //Reset headers to avoid OPTIONS request (aka preflight)
  13 + $httpProvider.defaults.headers.common = {};
  14 + $httpProvider.defaults.headers.post = {};
  15 + $httpProvider.defaults.headers.put = {};
  16 + $httpProvider.defaults.headers.patch = {};
  17 +
  18 + // $httpProvider.defaults.useXDomain = true;
  19 + // $httpProvider.defaults.headers.common = {Accept: 'application/json, text/plain, */*'};
  20 + // $httpProvider.defaults.headers.post = {'Content-Type': "application/json;charset=utf-8"};
  21 + $httpProvider.defaults.headers.post = {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'};
  22 + // $httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = '*';
  23 +
  24 + $httpProvider.interceptors.push([
  25 + '$injector',
  26 + function ($injector) {
  27 + return $injector.get('AuthInterceptor');
  28 + }
  29 + ]);
  30 +
  31 + }
  32 +
  33 + /** @ngInject */
9 34 function config($logProvider) {
  35 +
10 36 // Enable log
11 37 $logProvider.debugEnabled(true);
12   -
13   - // Set options third-party lib
14 38 }
15 39  
16 40 })();
... ...
src/app/index.constants.js
... ... @@ -6,11 +6,26 @@
6 6 .module('dialoga')
7 7 .constant('api', {
8 8 token: null,
9   - host: 'http://login.dialoga.gov.br/api/v1/',
  9 + // host: 'http://login.dialoga.gov.br/api/v1/',
  10 + host: 'http://www.participa.br/api/v1/',
10 11 articleId: {
11 12 home: 103358
12 13 }
13 14 })
  15 + .constant('AUTH_EVENTS', {
  16 + loginSuccess: 'auth-login-success',
  17 + loginFailed: 'auth-login-failed',
  18 + logoutSuccess: 'auth-logout-success',
  19 + sessionTimeout: 'auth-session-timeout',
  20 + notAuthenticated: 'auth-not-authenticated',
  21 + notAuthorized: 'auth-not-authorized'
  22 + })
  23 + .constant('USER_ROLES', {
  24 + all: '*',
  25 + admin: 'admin',
  26 + restrict: 'restrict',
  27 + visitor: 'visitor'
  28 + })
14 29 .constant('Modernizr', window.Modernizr)
15 30 .constant('jQuery', window.jQuery)
16 31 // .constant('key', value)
... ...
src/app/index.route.js
... ... @@ -20,6 +20,18 @@
20 20 'footer': { templateUrl: 'app/partials/footer/footer.html' }
21 21 }
22 22 })
  23 + .state('login', {
  24 + url: '/login',
  25 + views: {
  26 + 'header': { templateUrl: 'app/partials/header/header.html' },
  27 + 'main': {
  28 + templateUrl: 'app/partials/login/login.html',
  29 + controller: 'LoginController',
  30 + controllerAs: 'login'
  31 + },
  32 + 'footer': { templateUrl: 'app/partials/footer/footer.html' }
  33 + }
  34 + })
23 35 .state('programas', {
24 36 url: '/programas',
25 37 views: {
... ...
src/app/index.run.js
... ... @@ -4,12 +4,40 @@
4 4  
5 5 angular
6 6 .module('dialoga')
7   - .run(handleAccessibility)
  7 + .run(runAuth)
  8 + .run(runAccessibility)
8 9 .run(runBlock);
9 10  
10 11 /** @ngInject */
11   - function handleAccessibility($rootScope, $timeout, $cookies, $log) {
12   - $log.debug('handleAccessibility');
  12 + function runAuth($rootScope, $cookies, USER_ROLES, AUTH_EVENTS, AuthService, $log){
  13 +
  14 + // Listner url/state changes, and check permission
  15 + $rootScope.$on('$stateChangeStart', function (event, next) {
  16 + if(!next.data || !next.data.authorizedRoles){
  17 + $log.debug('public url/state');
  18 + return;
  19 + }
  20 +
  21 + var authorizedRoles = next.data.authorizedRoles;
  22 + if (!AuthService.isAuthorized(authorizedRoles)) {
  23 + event.preventDefault();
  24 + if (AuthService.isAuthenticated()) {
  25 + // user is not allowed
  26 + $log.debug('user is not allowed');
  27 + $rootScope.$broadcast(AUTH_EVENTS.notAuthorized);
  28 + } else {
  29 + // user is not logged in
  30 + $log.debug('user is not logged in');
  31 + $rootScope.$broadcast(AUTH_EVENTS.notAuthenticated);
  32 + }
  33 + }
  34 + });
  35 +
  36 + $log.debug('runAuth end.');
  37 + }
  38 +
  39 + /** @ngInject */
  40 + function runAccessibility($rootScope, $timeout, $cookies, $log) {
13 41  
14 42 var contrast = $cookies.get('dialoga_contraste') === "true";
15 43 adjustContrast(contrast);
... ... @@ -40,11 +68,13 @@
40 68 $log.warn('role="main" not found.');
41 69 }
42 70 };
  71 +
  72 + $log.debug('runAccessibility end.');
43 73 }
44 74  
45 75 /** @ngInject */
46 76 function runBlock($log) {
47   - $log.debug('runBlock');
  77 + $log.debug('runBlock end.');
48 78 }
49 79  
50 80 })();
... ...
src/app/partials/footer/footer.scss
... ... @@ -7,5 +7,5 @@ $barra-theme: ("green": #00420c, "yellow": #2c66ce, "blue": #0042b1);
7 7 }
8 8  
9 9 #footer {
10   - margin: 10px 0;
  10 + margin: 10px auto;
11 11 }
... ...
src/app/partials/login/login.controller.js 0 → 100644
... ... @@ -0,0 +1,44 @@
  1 +(function() {
  2 + 'use strict';
  3 +
  4 + angular
  5 + .module('dialoga')
  6 + .controller('LoginController', LoginController);
  7 +
  8 + /** @ngInject */
  9 + function LoginController($rootScope, AUTH_EVENTS, AuthService, Session, $log) {
  10 + $log.debug('LoginController');
  11 +
  12 + var vm = this;
  13 +
  14 + vm.$rootScope = $rootScope;
  15 + vm.AUTH_EVENTS = AUTH_EVENTS;
  16 + vm.AuthService = AuthService;
  17 + vm.Session = Session;
  18 + vm.$log = $log;
  19 +
  20 + vm.init();
  21 + }
  22 +
  23 + LoginController.prototype.init = function() {
  24 + var vm = this;
  25 +
  26 + // init variables
  27 + vm.credentials = {};
  28 +
  29 + // attach events
  30 +
  31 + // ...
  32 + };
  33 +
  34 + LoginController.prototype.login = function(credentials) {
  35 + var vm = this;
  36 +
  37 + vm.AuthService.login(credentials).then(function(user) {
  38 + // handle view
  39 + }, function() {
  40 + // handle view
  41 + });
  42 + };
  43 +
  44 +})();
... ...
src/app/partials/login/login.html 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +<section role="main" class="section-gray">
  2 + <div class="container">
  3 + <div class="row">
  4 + <div class="col-sm-8 col-sm-offset-2">
  5 + <form name="loginForm" ng-submit="login.login(login.credentials)">
  6 + <div class="form-group">
  7 + <label for="inputUsername" class="sr-only">E-mail:</label>
  8 + <div class="input-group">
  9 + <div class="input-group-addon"><span class="glyphicon glyphicon-user"></span></div>
  10 + <input type="text" id="inputUsername" class="form-control" placeholder="E-mail" required="" autofocus="" ng-model="login.credentials.username">
  11 + </div>
  12 + </div>
  13 + <div class="form-group">
  14 + <label for="inputPassword" class="sr-only">Senha:</label>
  15 + <div class="input-group">
  16 + <div class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></div>
  17 + <input type="password" id="inputPassword" class="form-control" placeholder="Senha" required="" ng-model="login.credentials.password">
  18 + </div>
  19 + </div>
  20 + <div class="form-group">
  21 + <button class="btn btn-lg btn-primary btn-block" type="submit">Entrar</button>
  22 + </div>
  23 + </form>
  24 + </div>
  25 + </div>
  26 + </div>
  27 +</section>
... ...
src/app/partials/login/signup.html 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +<section>
  2 + <div class="container">
  3 + <div class="row">
  4 + <div class="col-sm-12">
  5 + <h1>Cadastro</h1>
  6 + <p>Cadastre-se para fazer parte do Dialoga Brasil, interagir com as propostas e enviar as suas!</p>
  7 + </div>
  8 + </div>
  9 + </div>
  10 +</section>
  11 +<section role="main" class="section-gray">
  12 + <div class="container">
  13 + <div class="row">
  14 + <div class="col-sm-8 col-sm-offset-2">
  15 + <h2>Conecte-se por redes sociais</h2>
  16 + <div class="col-sm-6">
  17 + <button type="button" class="btn btn-lg btn-block btn-social btn-facebook">
  18 + <span class="glyphicon icon-facebook icon-white" aria-hidden="true">
  19 + <!-- Facebook -->
  20 + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 33 33" width="25" height="25"><path d="M18 32L12 32 12 16l-4 0 0-5.5 4 0 0-3.2C12 2.7 13.2 0 18.5 0l4.4 0 0 5.5 -2.8 0c-2.1 0-2.2 0.8-2.2 2.2l0 2.8 5 0 -0.6 5.5L18 16 18 32z"/></svg>
  21 +
  22 + <!-- Twitter -->
  23 + <!-- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 33 33" width="25" height="25"><path d="M32 6.1c-1.2 0.5-2.4 0.9-3.8 1 1.4-0.8 2.4-2.1 2.9-3.6 -1.3 0.8-2.7 1.3-4.2 1.6 -1.2-1.3-2.9-2.1-4.8-2.1 -3.6 0-6.6 2.9-6.6 6.6 0 0.5 0.1 1 0.2 1.5 -5.5-0.3-10.3-2.9-13.5-6.9 -0.6 1-0.9 2.1-0.9 3.3 0 2.3 1.2 4.3 2.9 5.5 -1.1 0-2.1-0.3-3-0.8 0 0 0 0.1 0 0.1 0 3.2 2.3 5.8 5.3 6.4 -0.6 0.2-1.1 0.2-1.7 0.2 -0.4 0-0.8 0-1.2-0.1 0.8 2.6 3.3 4.5 6.1 4.6 -2.2 1.8-5.1 2.8-8.2 2.8 -0.5 0-1.1 0-1.6-0.1 2.9 1.9 6.4 3 10.1 3 12.1 0 18.7-10 18.7-18.7 0-0.3 0-0.6 0-0.8C30 8.5 31.1 7.4 32 6.1z"/></svg> -->
  24 + </span>
  25 + <span class="text">
  26 + Conectar pelo Facebook
  27 + </span>
  28 + </button>
  29 + </div>
  30 + <div class="col-sm-6">
  31 + <button type="button" class="btn btn-lg btn-block btn-social btn-google-plus">
  32 + <span class="glyphicon icon-google-plus icon-white" aria-hidden="true">
  33 + <!-- Google + -->
  34 + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 33 33" width="25" height="25"><path d="M17.5 2c0 0-6.3 0-8.4 0C5.3 2 1.8 4.8 1.8 8.1c0 3.4 2.6 6.1 6.4 6.1 0.3 0 0.5 0 0.8 0 -0.2 0.5-0.4 1-0.4 1.6 0 0.9 0.5 1.7 1.1 2.3 -0.5 0-0.9 0-1.5 0C3.6 18.1 0 21.1 0 24.1c0 3 3.9 4.9 8.6 4.9 5.3 0 8.2-3 8.2-6 0-2.4-0.7-3.9-2.9-5.4 -0.8-0.5-2.2-1.8-2.2-2.6 0-0.9 0.3-1.3 1.6-2.4 1.4-1.1 2.4-2.6 2.4-4.4 0-2.1-0.9-4.2-2.7-4.8l2.7 0L17.5 2zM14.5 22.5c0.1 0.3 0.1 0.6 0.1 0.9 0 2.4-1.6 4.4-6.1 4.4 -3.2 0-5.5-2-5.5-4.5 0-2.4 2.9-4.4 6.1-4.4 0.8 0 1.4 0.1 2.1 0.3C12.9 20.4 14.2 21.1 14.5 22.5zM9.4 13.4c-2.2-0.1-4.2-2.4-4.6-5.2 -0.4-2.8 1.1-5 3.2-4.9 2.2 0.1 4.2 2.3 4.6 5.2C13 11.2 11.6 13.4 9.4 13.4zM26 8L26 2 24 2 24 8 18 8 18 10 24 10 24 16 26 16 26 10 32 10 32 8z"/></svg>
  35 + </span>
  36 + <span class="text">
  37 + Conectar pelo Google+
  38 + </span>
  39 + </button>
  40 + </div>
  41 + </div>
  42 + </div>
  43 + <div class="row">
  44 + <div class="col-sm-8 col-sm-offset-2">
  45 + <hr class="separator-or"></hr>
  46 + </div>
  47 + </div>
  48 + <div class="row">
  49 + <div class="col-sm-8 col-sm-offset-2">
  50 + <h2>Faça o cadastro abaixo</h2>
  51 + </div>
  52 + </div>
  53 + </div>
  54 +</section>
... ...
src/favicon.ico
No preview for this file type
src/index.html
... ... @@ -5,7 +5,7 @@
5 5 <!--[if gt IE 8]><!--> <html class="no-js" lang="pt-br" ng-app="dialoga"> <!--<![endif]-->
6 6 <head>
7 7 <meta charset="utf-8">
8   - <title>dialoga</title>
  8 + <title>Dialoga Brasil</title>
9 9 <meta name="description" content="">
10 10 <meta name="viewport" content="width=device-width">
11 11 <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
... ...