diff --git a/app/coffee/modules/auth.coffee b/app/coffee/modules/auth.coffee new file mode 100644 index 00000000..c8e9fd3c --- /dev/null +++ b/app/coffee/modules/auth.coffee @@ -0,0 +1,67 @@ +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino Garcia +# Copyright (C) 2014 David Barragán Merino +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +taiga = @.taiga + +class AuthService extends taiga.TaigaService + @.$inject = ["$rootScope", "$tgStorage", "$tgModel", "$tgHttp"] + + constructor: (@rootScope, @storage, @model, @http) -> + super() + + getUser: -> + userData = @storage.get("userInfo") + if userData + return @model.make_model("users", userData) + return null + + setUser: (user) -> + @rootScope.auth = user + @rootScope.$broadcast("i18n:change", user.default_language) + @gmStorage.set("userInfo", user.getAttrs()) + + clear: -> + @rootScope.auth = null + @gmStorage.remove("userInfo") + + setToken: (token) -> + @storage.set("token", token) + + getToken: -> + return @storage.get("token") + + login: (username, password) -> + url = @urls.resolve("auth") + + data = { + username: username + password: password + } + + return @http.post(url, data).then (data, status) => + user = @model.make_model("users", data) + @.setToken(data["auth_token"]) + @.setUser(user) + return user + + isAuthenticated: -> + if @.getUser() != null + return true + return false + +module = angular.module("taigaAuth", ["taigaResources"]) +module.service("$tgAuth", AuthService) diff --git a/app/coffee/modules/resources/http.coffee b/app/coffee/modules/resources/http.coffee new file mode 100644 index 00000000..4f0c691f --- /dev/null +++ b/app/coffee/modules/resources/http.coffee @@ -0,0 +1,67 @@ +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino Garcia +# Copyright (C) 2014 David Barragán Merino +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +taiga = @.taiga + +class HttpService extends taiga.TaigaService + @.$inject = ["$http", "$q", "$tgStorage"] + + headers: -> + token = @storage.get('token') + if token + return {"Authorization":"Bearer #{token}"} + return {} + + constructor: (@http, @q, @storage) -> + super() + + request: (options) -> + options.headers = _.merge({}, options.headers or {}, @.headers()) + if _.isPlainObject(options.data) + options.data = JSON.stringify(options.data) + + return @http(options) + + get: (url, params) -> + return @.request({ + method: "GET", + params: params + }) + + post: (url, data, params) -> + options = {method: "POST"} + options.data = data if data + options.params = params if params + return @.request(options) + + put: (url, data, params) -> + options = {method: "PUT"} + options.data = data if data + options.params = params if params + return @.request(options) + + patch: (url, data, params) -> + options = {method: "PATCH"} + options.data = data if data + options.params = params if params + return @.request(options) + + delete: (url, data, params) -> + options = {method: "DELETE"} + options.data = data if data + options.params = params if params + return @.request(options) diff --git a/app/coffee/modules/resources/init.coffee b/app/coffee/modules/resources/init.coffee index 4db1d1a2..662f6310 100644 --- a/app/coffee/modules/resources/init.coffee +++ b/app/coffee/modules/resources/init.coffee @@ -1,3 +1,20 @@ +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino Garcia +# Copyright (C) 2014 David Barragán Merino +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + init = (urls) -> urls.update({ "auth": "/api/v1/auth" diff --git a/app/coffee/modules/resources/model.coffee b/app/coffee/modules/resources/model.coffee index 86e99b99..6bdb9b60 100644 --- a/app/coffee/modules/resources/model.coffee +++ b/app/coffee/modules/resources/model.coffee @@ -115,15 +115,16 @@ class Model return model -provider = ($q, $http, $gmUrls, $gmStorage) -> - headers = -> - token = $gmStorage.get('token') - if token - return {"Authorization":"Bearer #{token}"} - return {} +taiga = @.taiga +class ModelService extends taiga.TaigaService + @.$inject = ["$q", "$tgUrls", "$tgStorage", "$tgHttp"] + + constructor: (@q, @urls, @storage, @http) -> + super() + +provider = ($q, $http, $gmUrls, $gmStorage) -> service = {} - service.headers = headers service.make_model = (name, data, cls=Model, dataTypes={}) -> return new cls(name, data, dataTypes) diff --git a/app/coffee/modules/resources/repository.coffee b/app/coffee/modules/resources/repository.coffee index 6de5e143..f6a3a50f 100644 --- a/app/coffee/modules/resources/repository.coffee +++ b/app/coffee/modules/resources/repository.coffee @@ -18,33 +18,20 @@ taiga = @.taiga class RepositoryService extends taiga.TaigaService - @.$inject = ["$http", "$q", "$tgModel", "$tgStorage"] + @.$inject = ["$q", "$tgModel", "$tgStorage", "$tgHttp"] - constructor: (@http, @q, @model, @storage) -> + constructor: (@q, @model, @storage, @http) -> super() - headers: -> - token = @.storage.get('token') - if token - return {"Authorization":"Bearer #{token}"} - return {} - resolveUrlForModel: (model) -> idAttrName = model.getIdAttrName() return "#{@urls.resolve(model.name)}/#{model[idAttrName]}" create: (name, data, dataTypes={}, extraParams={}) -> defered = @q.defer() + url = @urls.resolve(name) - params = { - method: "POST" - url: @urls.resolve(name) - headers: headers() - data: JSON.stringify(data) - params: extraParams - } - - promise = @http(params) + promise = @http.post(url, JSON.stringify(data)) promise.success (_data, _status) => defered.resolve(@model.make_model(name, _data, null, dataTypes)) @@ -55,14 +42,9 @@ class RepositoryService extends taiga.TaigaService remove: (model) -> defered = $q.defer() + url = @.resolveUrlForModel(model) - params = { - method: "DELETE" - url: @.resolveUrlForModel(model) - headers: @.headers() - } - - promise = @http(params) + promise = @http.delete(url) promise.success (data, status) -> defered.resolve(model) @@ -78,20 +60,14 @@ class RepositoryService extends taiga.TaigaService defered.resolve(model) return defered.promise - params = { - url: @.resolveUrlForModel(model) - headers: @.headers() - } + url = @.resolveUrlForModel(model) + data = JSON.stringify(model.getAttrs(patch)) if patch - params.method = "PATCH" + promise = @http.patch(url, data) else - params.method = "PUT" + promise = @http.put(url, data) - params.data = JSON.stringify(model.getAttrs(patch)) - params = _.extend({}, params, extraParams) - - promise = @http(params) promise.success (data, status) => model._isModified = false model._attrs = _.extend(model.getAttrs(), data) @@ -107,13 +83,9 @@ class RepositoryService extends taiga.TaigaService refresh: (model) -> defered = $q.defer() - params = { - method: "GET", - url: @.resolveUrlForModel(model) - headers: @.headers() - } - promise = @http(params) + url = @.resolveUrlForModel(model) + promise = @http.get(url) promise.success (data, status) -> model._modifiedAttrs = {} model._attrs = data @@ -127,5 +99,28 @@ class RepositoryService extends taiga.TaigaService return defered.promise + queryMany: (name, params) -> + url = @urls.resolve(name) + return @http.get(url, params).then (data, status) -> + return _.map(data, (x) -> @model.make_model(name, x)) + + queryOne: (name, id, params) -> + url = @urls.resolve(name) + url = "#{url}/#{id}" if id + + return @http.get(url, params).then (data, status) -> + return @model.make_model(name, data) + + queryPaginated: (name, params) -> + url = @urls.resolve(name) + return @http.get(url, params).then (data, status, headers) -> + headers = headers() + result = {} + result.models = _.map(data, (x) -> @model.make_model(name, x)) + result.count = parseInt(headers["x-pagination-count"], 10) + result.current = parseInt(headers["x-pagination-current"] or 1, 10) + result.paginatedBy = parseInt(headers["x-paginated-by"], 10) + return result + module = angular.module("taigaResources") -module.service("resources", RepositoryService) +module.service("$tgRepo", RepositoryService)