Copied to clipboard

Flag this post as spam?

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


  • Tim C 161 posts 528 karma points
    May 22, 2016 @ 08:57
    Tim C
    0

    Custom js back office access user type

    I know this is only javascript secure but I was thinking of creating a plugin with some javascript to hide some tabs or properties from non admin users.

    How easy is it to access the user type (ie Writer/Editor/Translator/Administrator) from javascript. I think this should be possible but not sure how.

    Thanks

    [update] tested plugins to make sure I could run javascript but it seems to run before the back office has finished loading controls and tabs. Is there any way (short of a brute force setTimeout) to delay until Umbraco has done its stuff?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 22, 2016 @ 10:11
    Jeavon Leopold
    100

    Hi Tim

    I did this before which should work for you:

    userService.getCurrentUser().then(function (user) { if (user.userType !== "admin") { hidesomething(); } });

    Jeavon

  • Tim C 161 posts 528 karma points
    May 22, 2016 @ 10:12
    Tim C
    0

    Thanks Jeavon

    As it looks like you were hiding something on the Umbraco canvas, did you find a way to delay until the controls were loaded?

  • Tim C 161 posts 528 karma points
    May 22, 2016 @ 10:15
    Tim C
    0

    Tried this

    Uncaught ReferenceError: userService is not defined

    What do I need to add to my js?

    Thanks

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 22, 2016 @ 10:25
    Jeavon Leopold
    0

    I was hiding within the editor so didn't have a issue. You need to add the userService to your function to be injected

    function myFunction($rootScope, $scope, userService) {

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 22, 2016 @ 10:32
  • Tim C 161 posts 528 karma points
    May 22, 2016 @ 10:45
    Tim C
    0

    Sorry for being dense, but I am trying to get to grips with this stuff.

    I tried putting this in my js file mentioned in the package.manifest

    alert('Yes, this happens');
    
    angular.module("umbraco")
       .controller(
            "test",
            function($rootScope,$scope,userService){
                userService.getCurrentUser().then(function (user) { 
                    alert('Test 1 ' + user.userType);
                });
    
                $scope.onEnd = function(){
                        $timeout(function(){
                            userService.getCurrentUser().then(function (user) { 
                                alert('Test 2 : ' + user.userType);
                            });
                        })
                }
            }
        );
    

    'Yes, this happens' popped up, but neither of the other alerts fired - I tried putting breakpoints in developer tools but it looks like the controller doesn't trigger .

    I have created custom property editors and the controller triggers there - not sure why this one doesn't?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 22, 2016 @ 13:15
    Jeavon Leopold
    0

    Assuming you have a view that utilises the controller?

  • Tim C 161 posts 528 karma points
    May 22, 2016 @ 13:31
    Tim C
    0

    How do you add a view : I have done it in custom property editors eg

    {
        propertyEditors:[
            {
                alias: "testEditor",
                name: "testEditor",
                valueType:"JSON",
                editor:{ 
                    view:"~/App_Plugins/myEditor/testEditor.html"
                },
    ...
    

    but this is for a plugin for the whole page not just a property editor : can I just add

    {
    view:"~/App_Plugins/test/test.html",
    javascript:[
            '~/App_Plugins/test/test.js'
        ]
    }
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 22, 2016 @ 13:45
    Jeavon Leopold
    0

    Something will need to load your view, is it a dashboard or tree or....?

  • Tim C 161 posts 528 karma points
    May 22, 2016 @ 14:40
    Tim C
    0

    All I want to do is have some javascript/css on every document editing form that has access to the userService - which I think (from what you have said) means I need to use angularjs and a controller.

    It wouldn't be tied to any property editor.

    Does that make any sense?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 22, 2016 @ 15:06
    Jeavon Leopold
    0

    Ok, got it. So you can get your js loading up via a package.manifest but you need a view to bind the controller, only way I think you can do that is to add/hack it into a existing core view, likely just needs to be a single div with ng-controller

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 22, 2016 @ 15:08
    Jeavon Leopold
    0

    Maybe something like /Umbraco/Views/Content/edit.html

  • Tim C 161 posts 528 karma points
    May 22, 2016 @ 16:06
    Tim C
    0

    I'm gonna try this.

    Thanks again.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies