Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Topic author was deleted

    Sep 23, 2014 @ 16:01

    Get Current User ID with JavaScript?

    Hi all,

    I'm looking to create something that can detect if a user is currently editing a file in the Umbraco back-end. Similar tools exist, but I can't find something that works nicely with Umbraco 7.

    I can tie into page open and save events through the JavaScript, but I'm not sure if I can access user details through it? There are places (I think in the umbraco.controllers.js, the "userService"?) that deal with users, but is it possible to tie into this easily?

    Thanks, Ed

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 23, 2014 @ 16:18
    Dan Lister
    0

    Hi Ed,

    I'm not sure if this will help but I know you can certainly use the User Service via a custom data type like as follow, assuming that you are using AngularJS:

    angular.module("umbraco").controller("MyController", function ($scope, userService) {
        $scope.user = userService.getCurrentUser();
    });
    

    Then within your view, you can then use the current user properties as follows:

    <div ng-controller="MyController">
        {{ user.id }} {{ user.name }}
    </div>
    

    Thanks, Dan.

  • Comment author was deleted

    Sep 23, 2014 @ 16:23

    Hi Dan,

    Thanks for your response!

    Well, I'm trying to get this information from one of the back-end pages, from the EditStyleSheet.js file. I'm getting an error that angular isn't defined (expected). Forgive me - new to Angular - is there something I'll need to include in this .js file?

    Thanks, Ed

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 23, 2014 @ 16:48
    Dan Lister
    0

    That makes two of us :)

    How are you registering your code?

    If I'm correct, you have to provide Umbraco with a package manifest in order to register components correctly and in the right order. So for example, within the folder /App_Data/MyPackage/, you would want to create a package.manifest file which could contain something like the following:

    {
        javascript: [
            '~/App_Plugins/MyPackage/MyController.js'
        ]
    }
    

    Hopefully it should point you in the direction.

    Thanks, Dan.

  • Comment author was deleted

    Sep 24, 2014 @ 11:07

    Morning!

    Thanks for that, Dan - looks like I'm getting somewhere!

    I've managed to move the code out into a separate controller in a custom package, used the package.manifest, and it's picking the code up nicely! But I'm still having trouble with "angular is not defined". Looking at the computed source, angular.min.js is definitely loaded before my controller (unless lazyload is doing crazy things), so I'm not sure why it can't be found...is this something to do with scope?

    Thanks, Ed

Please Sign in or register to post replies

Write your reply to:

Draft