From 419f1e5d630ff848f52183f20f9b2b3fbb2a4cc5 Mon Sep 17 00:00:00 2001 From: clemthenem Date: Tue, 5 Jul 2016 17:06:24 +1000 Subject: [PATCH 1/4] fixed a bug stopping the page to reload on staff changes --- .../app/components/mnoe-staffs-list/mnoe-staffs-list.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frontend-admin/src/app/components/mnoe-staffs-list/mnoe-staffs-list.coffee b/frontend-admin/src/app/components/mnoe-staffs-list/mnoe-staffs-list.coffee index 3c246ed72..3fc52987b 100644 --- a/frontend-admin/src/app/components/mnoe-staffs-list/mnoe-staffs-list.coffee +++ b/frontend-admin/src/app/components/mnoe-staffs-list/mnoe-staffs-list.coffee @@ -86,7 +86,7 @@ console.log 'Remove staff role:' + staff MnoeUsers.removeStaff(staff.id).then( -> toastr.success("#{staff.name} #{staff.surname} has been successfully removed.") - ) + ) ) # Fetch staffs @@ -102,11 +102,11 @@ fetchStaffs(vm.staff.nbItems, 0).then( -> # Notify me if a user is added MnoeObservables.registerCb(OBS_KEYS.staffAdded, -> - displayCurrentState() + fetchStaffs(vm.staff.nbItems, vm.staff.offset) ) # Notify me if the list changes MnoeObservables.registerCb(OBS_KEYS.staffChanged, -> - displayCurrentState() + fetchStaffs(vm.staff.nbItems, vm.staff.offset) ) ) return From f07b6f1ce29d1a9dd6e870cd6b45ed70309b0280 Mon Sep 17 00:00:00 2001 From: clemthenem Date: Tue, 5 Jul 2016 18:12:25 +1000 Subject: [PATCH 2/4] Added the authorization to access the staff page --- .../mnoe-api/admin/current-user.svc.coffee | 32 +++++++++++++++++++ .../mnoe-api/admin/users.svc.coffee | 6 ++-- frontend-admin/src/app/index.constants.coffee | 3 +- .../src/app/views/dashboard.controller.coffee | 8 ++++- .../src/app/views/dashboard.layout.html | 2 +- 5 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 frontend-admin/src/app/components/mnoe-api/admin/current-user.svc.coffee diff --git a/frontend-admin/src/app/components/mnoe-api/admin/current-user.svc.coffee b/frontend-admin/src/app/components/mnoe-api/admin/current-user.svc.coffee new file mode 100644 index 000000000..a6ddd5925 --- /dev/null +++ b/frontend-admin/src/app/components/mnoe-api/admin/current-user.svc.coffee @@ -0,0 +1,32 @@ +# Service to update the current user + +# We're not using angular-devise as the update functionality hasn't been +# merged yet. +# As we're using Devise + Her, we have custom routes to update the current user +# It then makes more sense to have an extra service rather than have customised +# fork of the upstream library + + +@App.service 'MnoeCurrentUser', (MnoeApiSvc, $window, $state) -> + _self = @ + + # Store the current_user promise + # Only one call will be executed even if there is multiple callers at the same time + userPromise = null + + # Save the current user in variable to be able to reference it directly + @user = {} + + # Get the current user admmin role + @getAdminRole = -> + return userPromise if userPromise? + userPromise = MnoeApiSvc.one('current_user').get().then( + (response) -> + + adminRole = {admin_role: response.data.admin_role} + + angular.copy(adminRole, _self.user) + response + ) + + return @ diff --git a/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee b/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee index 8479235c3..c0d34abe1 100644 --- a/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee +++ b/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee @@ -1,5 +1,5 @@ # Service for managing the users. -@App.service 'MnoeUsers', ($q, $log, MnoeAdminApiSvc, MnoeObservables, ADMIN_ROLES, OBS_KEYS) -> +@App.service 'MnoeUsers', ($q, $log, MnoeAdminApiSvc, MnoeObservables, OBS_KEYS) -> _self = @ @list = (limit, offset, sort) -> @@ -10,9 +10,9 @@ ) @staffs = (limit, offset, sort, params = {}) -> - # Require only users with an admin role + # Require only users with an admin role (gets any role, not necessarly defined in the frontend) if ! params['where[admin_role.in][]'] - params['where[admin_role.in][]'] = ADMIN_ROLES + params['where[admin_role.not]'] = '' return _getStaffs(limit, offset, sort, params) diff --git a/frontend-admin/src/app/index.constants.coffee b/frontend-admin/src/app/index.constants.coffee index ae47e4a71..5bdc968a3 100644 --- a/frontend-admin/src/app/index.constants.coffee +++ b/frontend-admin/src/app/index.constants.coffee @@ -1,6 +1,7 @@ @App .constant('USER_ROLES', ['Member', 'Power User', 'Admin', 'Super Admin']) - .constant('ADMIN_ROLES', ['Admin', 'Staff']) + .constant('ADMIN_ROLES', ['admin', 'staff']) # Must be lower case + .constant('STAFF_PAGE_AUTH', ['admin']) .constant('OBS_KEYS', { userChanged: 'userListChanged', staffChanged: 'staffListChanged', diff --git a/frontend-admin/src/app/views/dashboard.controller.coffee b/frontend-admin/src/app/views/dashboard.controller.coffee index 23e5ece41..97b854ed0 100644 --- a/frontend-admin/src/app/views/dashboard.controller.coffee +++ b/frontend-admin/src/app/views/dashboard.controller.coffee @@ -1,8 +1,9 @@ -@App.controller 'DashboardController', ($scope, $cookies, $sce, MnoeMarketplace, MnoErrorsHandler) -> +@App.controller 'DashboardController', ($scope, $cookies, $sce, MnoeMarketplace, MnoErrorsHandler, MnoeCurrentUser, STAFF_PAGE_AUTH) -> 'ngInject' main = this main.errorHandler = MnoErrorsHandler + main.staffPageAuthorized = STAFF_PAGE_AUTH main.trustSrc = (src) -> $sce.trustAsResourceUrl(src) @@ -32,4 +33,9 @@ # Marketplace is cached # MnoeMarketplace.getApps() + MnoeCurrentUser.getAdminRole().then( + # Display the layout + main.user = MnoeCurrentUser.user + ) + return diff --git a/frontend-admin/src/app/views/dashboard.layout.html b/frontend-admin/src/app/views/dashboard.layout.html index 450138aec..a5c91e432 100644 --- a/frontend-admin/src/app/views/dashboard.layout.html +++ b/frontend-admin/src/app/views/dashboard.layout.html @@ -19,7 +19,7 @@ - From bb686a335d2e21392918901026edd0d522d9f462 Mon Sep 17 00:00:00 2001 From: clemthenem Date: Tue, 5 Jul 2016 18:13:11 +1000 Subject: [PATCH 3/4] Added an invitation email when a staff is added --- .../mnoe-api/admin/users.svc.coffee | 2 +- .../create-staff.controller.coffee | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee b/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee index c0d34abe1..a18374cfc 100644 --- a/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee +++ b/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee @@ -49,7 +49,7 @@ # Update the admin-role of a staff to nothing # UPDATE /mnoe/jpi/v1/admin/users/:id @removeStaff = (id) -> - promise = MnoeAdminApiSvc.one('users', id).patch({admin_role: ""}).then( + promise = MnoeAdminApiSvc.one('users', id).remove().then( (response) -> MnoeObservables.notifyObservers(OBS_KEYS.staffChanged, promise) (error) -> diff --git a/frontend-admin/src/app/views/staff/create-staff-modal/create-staff.controller.coffee b/frontend-admin/src/app/views/staff/create-staff-modal/create-staff.controller.coffee index cb7e041cf..2d77d78dc 100644 --- a/frontend-admin/src/app/views/staff/create-staff-modal/create-staff.controller.coffee +++ b/frontend-admin/src/app/views/staff/create-staff-modal/create-staff.controller.coffee @@ -8,9 +8,10 @@ vm.isLoading = true MnoeUsers.addStaff(vm.user).then( (success) -> - toastr.success("#{vm.user.name} #{vm.user.surname} has been successfully added.") - # Close the modal returning the item to the parent window - $uibModalInstance.close(success.data) + + # Send an email to the new staff + sendConfirmationEmail(vm.user) + (error) -> toastr.error("An error occurred while adding #{vm.user.name} #{vm.user.surname}.") $log.error("An error occurred:", error) @@ -19,4 +20,25 @@ vm.onCancel = () -> $uibModalInstance.dismiss('cancel') + sendConfirmationEmail = (user) -> + MnoeUsers.sendSignupEmail(user.email).then( + (success) -> + toastr.success("#{user.name} #{user.surname} has been successfully added.") + + # Close the modal returning the item to the parent window + $uibModalInstance.close(success.data) + + (error) -> + toastr.error("An error occurred while sending an email to #{user.email}.") + $log.error("An error occurred:", error) + + # Remove the staff that has been added + MnoeUsers.removeStaff(vm.user.id).then( + (success) -> + (error) -> + toastr.error("An error occurred while deleting the user.") + $log.error("An error occurred:", error) + ) + ) + return From 05ca7e2a52ae53c3c75bf6bc95414a7c307a7a61 Mon Sep 17 00:00:00 2001 From: clemthenem Date: Wed, 6 Jul 2016 16:19:09 +1000 Subject: [PATCH 4/4] Fixed code based on Ouranos suggestions --- .../src/app/components/mnoe-api/admin/current-user.svc.coffee | 2 -- .../src/app/components/mnoe-api/admin/users.svc.coffee | 2 +- .../staff/create-staff-modal/create-staff.controller.coffee | 4 ---- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/frontend-admin/src/app/components/mnoe-api/admin/current-user.svc.coffee b/frontend-admin/src/app/components/mnoe-api/admin/current-user.svc.coffee index a6ddd5925..22d57797e 100644 --- a/frontend-admin/src/app/components/mnoe-api/admin/current-user.svc.coffee +++ b/frontend-admin/src/app/components/mnoe-api/admin/current-user.svc.coffee @@ -22,9 +22,7 @@ return userPromise if userPromise? userPromise = MnoeApiSvc.one('current_user').get().then( (response) -> - adminRole = {admin_role: response.data.admin_role} - angular.copy(adminRole, _self.user) response ) diff --git a/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee b/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee index a18374cfc..c0d34abe1 100644 --- a/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee +++ b/frontend-admin/src/app/components/mnoe-api/admin/users.svc.coffee @@ -49,7 +49,7 @@ # Update the admin-role of a staff to nothing # UPDATE /mnoe/jpi/v1/admin/users/:id @removeStaff = (id) -> - promise = MnoeAdminApiSvc.one('users', id).remove().then( + promise = MnoeAdminApiSvc.one('users', id).patch({admin_role: ""}).then( (response) -> MnoeObservables.notifyObservers(OBS_KEYS.staffChanged, promise) (error) -> diff --git a/frontend-admin/src/app/views/staff/create-staff-modal/create-staff.controller.coffee b/frontend-admin/src/app/views/staff/create-staff-modal/create-staff.controller.coffee index 2d77d78dc..6bc4cd5ec 100644 --- a/frontend-admin/src/app/views/staff/create-staff-modal/create-staff.controller.coffee +++ b/frontend-admin/src/app/views/staff/create-staff-modal/create-staff.controller.coffee @@ -8,10 +8,8 @@ vm.isLoading = true MnoeUsers.addStaff(vm.user).then( (success) -> - # Send an email to the new staff sendConfirmationEmail(vm.user) - (error) -> toastr.error("An error occurred while adding #{vm.user.name} #{vm.user.surname}.") $log.error("An error occurred:", error) @@ -24,10 +22,8 @@ MnoeUsers.sendSignupEmail(user.email).then( (success) -> toastr.success("#{user.name} #{user.surname} has been successfully added.") - # Close the modal returning the item to the parent window $uibModalInstance.close(success.data) - (error) -> toastr.error("An error occurred while sending an email to #{user.email}.") $log.error("An error occurred:", error)