From 481a36501f96d18d2a42d5ddd493a94e92d9f281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Sat, 14 Jan 2017 09:14:42 +0100 Subject: [PATCH] Refactored WhoAmI view and startup process. --- openslides/core/static/js/core/base.js | 58 +++++---------------- openslides/core/static/js/core/site.js | 19 +------ openslides/core/static/js/core/start.js | 64 ++++++++++++++++++++++++ openslides/users/access_permissions.py | 2 + openslides/users/static/js/users/base.js | 60 ---------------------- openslides/users/static/js/users/site.js | 22 -------- openslides/users/views.py | 11 +++- 7 files changed, 89 insertions(+), 147 deletions(-) create mode 100644 openslides/core/static/js/core/start.js diff --git a/openslides/core/static/js/core/base.js b/openslides/core/static/js/core/base.js index bed46f257..c30b81050 100644 --- a/openslides/core/static/js/core/base.js +++ b/openslides/core/static/js/core/base.js @@ -69,26 +69,25 @@ angular.module('OpenSlidesApp.core', [ } var websocketPath; - if (REALM == 'site') { + if (REALM === 'site') { websocketPath = '/ws/site/'; - } else if (REALM == 'projector') { + } else if (REALM === 'projector') { websocketPath = '/ws/projector/' + ProjectorID() + '/'; } else { console.error('The constant REALM is not set properly.'); } - var Autoupdate = { - messageReceivers: [], - onMessage: function (receiver) { - this.messageReceivers.push(receiver); - }, - reconnect: function () { - if (socket) { - socket.close(); - } + var Autoupdate = {}; + Autoupdate.messageReceivers = []; + Autoupdate.onMessage = function (receiver) { + Autoupdate.messageReceivers.push(receiver); + }; + Autoupdate.reconnect = function () { + if (socket) { + socket.close(); } }; - var newConnect = function () { + Autoupdate.newConnect = function () { socket = new WebSocket(websocketProtocol + '//' + location.host + websocketPath); clearInterval(recInterval); socket.onopen = function () { @@ -98,7 +97,7 @@ angular.module('OpenSlidesApp.core', [ $rootScope.connected = false; socket = null; recInterval = setInterval(function () { - newConnect(); + Autoupdate.newConnect(); }, 1000); }; socket.onmessage = function (event) { @@ -107,8 +106,6 @@ angular.module('OpenSlidesApp.core', [ }); }; }; - - newConnect(); return Autoupdate; } ]) @@ -301,37 +298,6 @@ angular.module('OpenSlidesApp.core', [ } ]) -.factory('loadGlobalData', [ - 'ChatMessage', - 'Config', - 'Projector', - 'ProjectorMessage', - 'Countdown', - function (ChatMessage, Config, Projector, ProjectorMessage, Countdown) { - return function () { - Config.findAll(); - - // Loads all projector data and the projectiondefaults - Projector.findAll(); - ProjectorMessage.findAll(); - Countdown.findAll(); - - // Loads all chat messages data and their user_ids - // TODO: add permission check if user has required chat permission - // error if include 'operator' here: - // "Circular dependency found: loadGlobalData <- operator <- loadGlobalData" - //if (operator.hasPerms("core.can_use_chat")) { - ChatMessage.findAll().then( function(chatmessages) { - angular.forEach(chatmessages, function (chatmessage) { - ChatMessage.loadRelations(chatmessage, 'user'); - }); - }); - //} - }; - } -]) - - // Template hooks .factory('templateHooks', [ diff --git a/openslides/core/static/js/core/site.js b/openslides/core/static/js/core/site.js index ee3276337..90a2aebef 100644 --- a/openslides/core/static/js/core/site.js +++ b/openslides/core/static/js/core/site.js @@ -80,7 +80,7 @@ angular.module('OpenSlidesApp.core.site', [ var that = this; this.scope = scope; this.updateMainMenu(); - operator.onOperatorChange(function () {that.updateMainMenu();}); + // TODO: operator.onOperatorChange(function () {that.updateMainMenu();}); }, updateMainMenu: function () { this.scope.elements = this.getElements(); @@ -100,15 +100,6 @@ angular.module('OpenSlidesApp.core.site', [ } ]) -// Load the global data when the operator changes -.run([ - 'loadGlobalData', - 'operator', - function (loadGlobalData, operator) { - operator.onOperatorChange(loadGlobalData); - } -]) - .run([ 'editableOptions', 'gettext', @@ -616,14 +607,6 @@ angular.module('OpenSlidesApp.core.site', [ } ]) -// Load the global data on startup -.run([ - 'loadGlobalData', - function(loadGlobalData) { - loadGlobalData(); - } -]) - // html-tag os-form-field to generate generic from fields // TODO: make it possible to use other fields then config fields .directive('osFormField', [ diff --git a/openslides/core/static/js/core/start.js b/openslides/core/static/js/core/start.js new file mode 100644 index 000000000..3bbe85344 --- /dev/null +++ b/openslides/core/static/js/core/start.js @@ -0,0 +1,64 @@ +(function () { + +'use strict'; + +angular.module('OpenSlidesApp.core.start', []) + +.run([ + '$http', + '$rootScope', + '$state', + 'autoupdate', + 'operator', + function($http, $rootScope, $state, autoupdate, operator) { + // Put the operator into the root scope + $http.get('/users/whoami/').success(function(data) { + $rootScope.guest_enabled = data.guest_enabled; + if (data.user_id === null && !data.guest_enabled) { + // Redirect to login dialog if user is not logged in. + $state.go('login', {guest_enabled: data.guest_enabled}); + } else { + autoupdate.newConnect(); + // TODO: Connect websocket + // Then operator.setUser(data.user_id, data.user); $rootScope.operator = operator; + } + }); + } +]) + +.factory('operator', [ + 'Group', + 'User', + function (User, Group) { + var operator = { + user: null, + perms: [], + isAuthenticated: function () { + return !!this.user; + }, + setUser: function(user_id, user_data) { + if (user_id && user_data) { + operator.user = User.inject(user_data); + operator.perms = operator.user.getPerms(); + } else { + operator.user = null; + operator.perms = Group.get(1).permissions; + } + }, + // Returns true if the operator has at least one perm of the perms-list. + hasPerms: function(perms) { + if (typeof perms === 'string') { + perms = perms.split(' '); + } + return _.intersection(perms, operator.perms).length > 0; + }, + // Returns true if the operator is a member of group. + isInGroup: function(group) { + return _.indexOf(operator.user.groups_id, group.id) > -1; + }, + }; + return operator; + } +]) + +}()); diff --git a/openslides/users/access_permissions.py b/openslides/users/access_permissions.py index e99b3713a..6c7225fc7 100644 --- a/openslides/users/access_permissions.py +++ b/openslides/users/access_permissions.py @@ -42,6 +42,8 @@ class UserAccessPermissions(BaseAccessPermissions): case = MANY_DATA else: case = LITTLE_DATA + elif user.pk == full_data.get('id'): + case = LITTLE_DATA else: case = NO_DATA diff --git a/openslides/users/static/js/users/base.js b/openslides/users/static/js/users/base.js index 79f9c5540..c08351892 100644 --- a/openslides/users/static/js/users/base.js +++ b/openslides/users/static/js/users/base.js @@ -4,66 +4,6 @@ angular.module('OpenSlidesApp.users', []) -.factory('operator', [ - 'User', - 'Group', - 'loadGlobalData', - 'autoupdate', - 'DS', - function (User, Group, loadGlobalData, autoupdate, DS) { - var operatorChangeCallbacks = [autoupdate.reconnect]; - var operator = { - user: null, - perms: [], - isAuthenticated: function () { - return !!this.user; - }, - onOperatorChange: function (func) { - operatorChangeCallbacks.push(func); - }, - setUser: function(user_id) { - if (user_id) { - User.find(user_id).then(function(user) { - operator.user = user; - // TODO: load only the needed groups - Group.findAll().then(function() { - operator.perms = user.getPerms(); - _.forEach(operatorChangeCallbacks, function (callback) { - callback(); - }); - }); - }); - } else { - operator.user = null; - operator.perms = []; - DS.clear(); - _.forEach(operatorChangeCallbacks, function (callback) { - callback(); - }); - Group.find(1).then(function(group) { - operator.perms = group.permissions; - _.forEach(operatorChangeCallbacks, function (callback) { - callback(); - }); - }); - } - }, - // Returns true if the operator has at least one perm of the perms-list. - hasPerms: function(perms) { - if (typeof perms == 'string') { - perms = perms.split(' '); - } - return _.intersection(perms, operator.perms).length > 0; - }, - // Returns true if the operator is a member of group. - isInGroup: function(group) { - return _.indexOf(operator.user.groups_id, group.id) > -1; - }, - }; - return operator; - } -]) - .factory('User', [ 'DS', 'Group', diff --git a/openslides/users/static/js/users/site.js b/openslides/users/static/js/users/site.js index 0ead12237..71060f38b 100644 --- a/openslides/users/static/js/users/site.js +++ b/openslides/users/static/js/users/site.js @@ -162,28 +162,6 @@ angular.module('OpenSlidesApp.users.site', [ } ]) -.run([ - 'operator', - '$rootScope', - '$http', - '$state', - 'Group', - function(operator, $rootScope, $http, $state, Group) { - // Put the operator into the root scope - $http.get('/users/whoami/').success(function(data) { - operator.setUser(data.user_id); - $rootScope.guest_enabled = data.guest_enabled; - if (data.user_id === null && !data.guest_enabled) { - // redirect to login dialog if use is not logged in - $state.go('login', {guest_enabled: data.guest_enabled}); - } - }); - $rootScope.operator = operator; - // Load all Groups. They are needed later - Group.findAll(); - } -]) - /* * Directive to check for permissions * diff --git a/openslides/users/views.py b/openslides/users/views.py index 93e8f23c4..e7c993f61 100644 --- a/openslides/users/views.py +++ b/openslides/users/views.py @@ -13,6 +13,7 @@ from ..utils.rest_api import ( detail_route, status, ) +from ..utils.collection import CollectionElement from ..utils.views import APIView from .access_permissions import GroupAccessPermissions, UserAccessPermissions from .models import Group, User @@ -234,10 +235,18 @@ class WhoAmIView(APIView): """ Appends the user id to the context. Uses None for the anonymous user. Appends also a flag if guest users are enabled in the config. + Appends also the serialized user if available. """ + user_id = self.request.user.pk + if self.request.user.pk is not None: + user_collection = CollectionElement.from_instance(self.request.user) + user_data = user_collection.as_dict_for_user(self.request.user) + else: + user_data = None return super().get_context_data( - user_id=self.request.user.pk, + user_id=user_id, guest_enabled=config['general_system_enable_anonymous'], + user=user_data, **context)