├── .gitignore ├── LICENSE ├── README.md └── ngDrupal.js /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components/* 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 alexpsi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ngDrupal 2 | ======== 3 | 4 | (Version 0.1.2 Very very alpha version) 5 | 6 | Angular service that allows Restangular to connect to a Drupal site utilizing Services 3.5+. Provides authentication and file upload methods. 7 | 8 | # Installation 9 | 10 | Download source from bower 11 | 12 | ````bash 13 | bower install angular-drupal -S 14 | ```` 15 | 16 | Add Restangular and ngDrupal to app and configure REST endpoint. 17 | 18 | ```` 19 | angular.module('myApp', [ 20 | 'restangular', 21 | 'ngDrupal' 22 | ... 23 | ]) 24 | .config(function (ngDrupalProvider) { 25 | ngDrupalProvider.config({ 26 | hostname: 'http://hostname/', 27 | endpoint: 'api' 28 | }); 29 | }) 30 | ```` 31 | 32 | And finally inject ngDrupal to your controller 33 | 34 | ```` 35 | controller('MainCtrl', function ($scope, ngDrupal) {}); 36 | ``` 37 | 38 | 39 | At server side, you can provision a new drupal installation by using the [feature branch] (https://github.com/alexpsi/ng-drupal/tree/feature) 40 | 41 | # Usage 42 | 43 | All ngDrupal methods return promises. 44 | 45 | ## Login 46 | 47 | ```` 48 | ngDrupal.login({username: username, password: password}).then(function(data) {console.log(data);}); 49 | ```` 50 | 51 | ## Logout 52 | 53 | ```` 54 | ngDrupal.logout(); 55 | ```` 56 | 57 | ## System connect 58 | Returns current user information 59 | ```` 60 | ngDrupal.systemConnect().then(function(data) {console.log(data);}); 61 | ```` 62 | 63 | ## NodeFactory 64 | Returns a Restangular object pointing to api/node 65 | ```` 66 | var node = ngDrupal.nodeFactory(); 67 | ```` 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /ngDrupal.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @ngdoc function 5 | * @name ngDrupal 6 | * @description 7 | * # ngDrupal 8 | * Angular service that facilitates connecting Restangular to a Drupal Services REST API 9 | * 10 | */ 11 | 12 | angular.module('ngDrupal', ['restangular']) 13 | .provider('ngDrupal', function() { 14 | // In the provider function, you cannot inject any 15 | // service or factory. This can only be done at the 16 | // "$get" method. 17 | 18 | this.hostname = ''; 19 | this.endpoint = ''; 20 | 21 | this.config = function(options) { 22 | this.hostname = options.hostname; 23 | this.endpoint = options.endpoint; 24 | }; 25 | 26 | 27 | 28 | 29 | this.$get = function($q, $http, Restangular) { 30 | 31 | var SITE_ROOT = this.hostname; 32 | var SERVICES_ENDPOINT = this.endpoint; 33 | console.log(SITE_ROOT); 34 | var REST_PATH = SITE_ROOT + SERVICES_ENDPOINT + '/'; 35 | Restangular.setBaseUrl(REST_PATH); 36 | Restangular.setDefaultHttpFields({withCredentials: true}); 37 | //console.log(REST_PATH); 38 | return { 39 | 40 | getCsrfToken: function() { 41 | 42 | var deferred = $q.defer(); 43 | $http({ 44 | url: SITE_ROOT + 'services/session/token', 45 | method: 'GET', 46 | withCredentials: true 47 | }).success(function(data /*, status, headers, config*/) { 48 | $http.defaults.headers.common['X-CSRF-Token'] = data; 49 | deferred.resolve(data); 50 | }).error(function(data /*, status, headers, config*/) { 51 | deferred.reject(data); 52 | }); 53 | return deferred.promise; 54 | }, 55 | 56 | systemConnect: function() { 57 | 58 | var deferred = $q.defer(); 59 | var url = REST_PATH + 'system/connect'; 60 | this.getCsrfToken().then(function() { 61 | $http({ 62 | url: url, 63 | method: 'POST', 64 | withCredentials: true 65 | }) 66 | .success(function(data /*, status, headers, config */) { 67 | deferred.resolve(data.user); 68 | }) 69 | .error(function(data /*, status, headers, config */) { 70 | deferred.reject(data); 71 | }); 72 | }); 73 | return deferred.promise; 74 | }, 75 | 76 | logout: function() { 77 | 78 | var that = this; 79 | var deferred = $q.defer(); 80 | var url = REST_PATH + 'user/logout'; 81 | 82 | $http({ 83 | url: url, 84 | method: 'POST', 85 | withCredentials: true 86 | }) 87 | .success(function(data /*, status, headers, config */) { 88 | that.getCsrfToken().then(function() { 89 | deferred.resolve(data); 90 | }); 91 | 92 | }) 93 | .error(function(data /*, status, headers, config */) { 94 | deferred.reject(data); 95 | }); 96 | 97 | return deferred.promise; 98 | }, 99 | 100 | login: function (username, password) { 101 | 102 | var that = this; 103 | var deferred = $q.defer(); 104 | var url = REST_PATH + 'user/login'; 105 | var user = { 106 | username: username, 107 | password: password 108 | }; 109 | 110 | $http({ 111 | url: url, 112 | method: 'POST', 113 | data: user, 114 | withCredentials: true, 115 | }) 116 | .success(function(data /*, status, headers, config */) { 117 | that.getCsrfToken().then(function() { 118 | deferred.resolve(data); 119 | }); 120 | 121 | }) 122 | .error(function(data /*, status, headers, config */) { 123 | deferred.reject(data); 124 | }); 125 | 126 | return deferred.promise; 127 | }, 128 | 129 | nodeFactory: function() { 130 | return Restangular.all('node'); 131 | }, 132 | 133 | structureField: function(value, _label) { 134 | // record optional label string or default to "value" 135 | var label = _label || "value"; 136 | 137 | if (isArray(value)) { 138 | var field_array = []; 139 | for (var i= 0, l=value.length; i