Vue.js with Laravel Permission










3















I am in the process of integrating Laravel Permission API with Vue.JS frontend. I am using https://github.com/spatie/laravel-permission library for Laravel Permission. I am not understanding how can I check permission in the Vue JS front End (In Laravel blade I am using @Can to check the permission).










share|improve this question
























  • I am also facing the same problem.

    – Tejas Khutale
    Nov 15 '18 at 5:25















3















I am in the process of integrating Laravel Permission API with Vue.JS frontend. I am using https://github.com/spatie/laravel-permission library for Laravel Permission. I am not understanding how can I check permission in the Vue JS front End (In Laravel blade I am using @Can to check the permission).










share|improve this question
























  • I am also facing the same problem.

    – Tejas Khutale
    Nov 15 '18 at 5:25













3












3








3








I am in the process of integrating Laravel Permission API with Vue.JS frontend. I am using https://github.com/spatie/laravel-permission library for Laravel Permission. I am not understanding how can I check permission in the Vue JS front End (In Laravel blade I am using @Can to check the permission).










share|improve this question
















I am in the process of integrating Laravel Permission API with Vue.JS frontend. I am using https://github.com/spatie/laravel-permission library for Laravel Permission. I am not understanding how can I check permission in the Vue JS front End (In Laravel blade I am using @Can to check the permission).







laravel vue.js user-permissions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 10 at 19:00









Pierce O'Neill

329514




329514










asked Nov 15 '18 at 5:17









Kapil YamakanmardiKapil Yamakanmardi

435




435












  • I am also facing the same problem.

    – Tejas Khutale
    Nov 15 '18 at 5:25

















  • I am also facing the same problem.

    – Tejas Khutale
    Nov 15 '18 at 5:25
















I am also facing the same problem.

– Tejas Khutale
Nov 15 '18 at 5:25





I am also facing the same problem.

– Tejas Khutale
Nov 15 '18 at 5:25












2 Answers
2






active

oldest

votes


















3














I'm literally working on this exact same thing. I'm thinking of adding a custom Vue directive that would check against the Laravel.permissions array.



It might even be as simple as



 Vue.directive('can', function (el, binding) 
return Laravel.permissions.indexOf(binding) !== -1;
)


I haven't tested this code. Just brainstorming here.



<button v-can="editStuff">You can edit this thing</button>


I can hold permissions this way:



window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
'userId' => Auth::user()->id,
'permissions' => Auth::user()->permissions()->pluck('name')->toArray()
]); ?>





share|improve this answer

























  • How you get laravel permission array in vue js?

    – Tejas Khutale
    Nov 15 '18 at 5:56






  • 2





    I guess you'd just use <script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script> somewhere in a blade template (most likely layout.blade.php).

    – Namoshek
    Feb 8 at 10:22






  • 2





    I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.

    – wheelmaker
    Feb 11 at 17:40






  • 1





    @wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout

    – Michael Mano
    Feb 11 at 21:36


















3





+50









I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.



Routes:



Route::get('/permission/permissionName', 'PermissionController@check');


Controller:



function check($permissionName) 
if (! Auth::user()->hasPermissionTo($permissionName))
abort(403);

return response('', 204);



Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.



Vue.mixin("can", (permissionName) => 
let hasAccess;
axios.get(`/permission/$permissionName`)
.then(()=>
hasAccess = true;

.catch(()=>
hasAccess = false;
;
return hasAccess;
);



And then everytime you wanna check permission, you can just do



<el-input v-if="can('write-stuff')"> </el-input>





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312875%2fvue-js-with-laravel-permission%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    I'm literally working on this exact same thing. I'm thinking of adding a custom Vue directive that would check against the Laravel.permissions array.



    It might even be as simple as



     Vue.directive('can', function (el, binding) 
    return Laravel.permissions.indexOf(binding) !== -1;
    )


    I haven't tested this code. Just brainstorming here.



    <button v-can="editStuff">You can edit this thing</button>


    I can hold permissions this way:



    window.Laravel = <?php echo json_encode([
    'csrfToken' => csrf_token(),
    'userId' => Auth::user()->id,
    'permissions' => Auth::user()->permissions()->pluck('name')->toArray()
    ]); ?>





    share|improve this answer

























    • How you get laravel permission array in vue js?

      – Tejas Khutale
      Nov 15 '18 at 5:56






    • 2





      I guess you'd just use <script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script> somewhere in a blade template (most likely layout.blade.php).

      – Namoshek
      Feb 8 at 10:22






    • 2





      I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.

      – wheelmaker
      Feb 11 at 17:40






    • 1





      @wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout

      – Michael Mano
      Feb 11 at 21:36















    3














    I'm literally working on this exact same thing. I'm thinking of adding a custom Vue directive that would check against the Laravel.permissions array.



    It might even be as simple as



     Vue.directive('can', function (el, binding) 
    return Laravel.permissions.indexOf(binding) !== -1;
    )


    I haven't tested this code. Just brainstorming here.



    <button v-can="editStuff">You can edit this thing</button>


    I can hold permissions this way:



    window.Laravel = <?php echo json_encode([
    'csrfToken' => csrf_token(),
    'userId' => Auth::user()->id,
    'permissions' => Auth::user()->permissions()->pluck('name')->toArray()
    ]); ?>





    share|improve this answer

























    • How you get laravel permission array in vue js?

      – Tejas Khutale
      Nov 15 '18 at 5:56






    • 2





      I guess you'd just use <script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script> somewhere in a blade template (most likely layout.blade.php).

      – Namoshek
      Feb 8 at 10:22






    • 2





      I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.

      – wheelmaker
      Feb 11 at 17:40






    • 1





      @wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout

      – Michael Mano
      Feb 11 at 21:36













    3












    3








    3







    I'm literally working on this exact same thing. I'm thinking of adding a custom Vue directive that would check against the Laravel.permissions array.



    It might even be as simple as



     Vue.directive('can', function (el, binding) 
    return Laravel.permissions.indexOf(binding) !== -1;
    )


    I haven't tested this code. Just brainstorming here.



    <button v-can="editStuff">You can edit this thing</button>


    I can hold permissions this way:



    window.Laravel = <?php echo json_encode([
    'csrfToken' => csrf_token(),
    'userId' => Auth::user()->id,
    'permissions' => Auth::user()->permissions()->pluck('name')->toArray()
    ]); ?>





    share|improve this answer















    I'm literally working on this exact same thing. I'm thinking of adding a custom Vue directive that would check against the Laravel.permissions array.



    It might even be as simple as



     Vue.directive('can', function (el, binding) 
    return Laravel.permissions.indexOf(binding) !== -1;
    )


    I haven't tested this code. Just brainstorming here.



    <button v-can="editStuff">You can edit this thing</button>


    I can hold permissions this way:



    window.Laravel = <?php echo json_encode([
    'csrfToken' => csrf_token(),
    'userId' => Auth::user()->id,
    'permissions' => Auth::user()->permissions()->pluck('name')->toArray()
    ]); ?>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 28 '18 at 6:15

























    answered Nov 15 '18 at 5:28









    Ismoil ShifoevIsmoil Shifoev

    1,3601914




    1,3601914












    • How you get laravel permission array in vue js?

      – Tejas Khutale
      Nov 15 '18 at 5:56






    • 2





      I guess you'd just use <script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script> somewhere in a blade template (most likely layout.blade.php).

      – Namoshek
      Feb 8 at 10:22






    • 2





      I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.

      – wheelmaker
      Feb 11 at 17:40






    • 1





      @wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout

      – Michael Mano
      Feb 11 at 21:36

















    • How you get laravel permission array in vue js?

      – Tejas Khutale
      Nov 15 '18 at 5:56






    • 2





      I guess you'd just use <script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script> somewhere in a blade template (most likely layout.blade.php).

      – Namoshek
      Feb 8 at 10:22






    • 2





      I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.

      – wheelmaker
      Feb 11 at 17:40






    • 1





      @wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout

      – Michael Mano
      Feb 11 at 21:36
















    How you get laravel permission array in vue js?

    – Tejas Khutale
    Nov 15 '18 at 5:56





    How you get laravel permission array in vue js?

    – Tejas Khutale
    Nov 15 '18 at 5:56




    2




    2





    I guess you'd just use <script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script> somewhere in a blade template (most likely layout.blade.php).

    – Namoshek
    Feb 8 at 10:22





    I guess you'd just use <script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script> somewhere in a blade template (most likely layout.blade.php).

    – Namoshek
    Feb 8 at 10:22




    2




    2





    I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.

    – wheelmaker
    Feb 11 at 17:40





    I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.

    – wheelmaker
    Feb 11 at 17:40




    1




    1





    @wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout

    – Michael Mano
    Feb 11 at 21:36





    @wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout

    – Michael Mano
    Feb 11 at 21:36













    3





    +50









    I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.



    Routes:



    Route::get('/permission/permissionName', 'PermissionController@check');


    Controller:



    function check($permissionName) 
    if (! Auth::user()->hasPermissionTo($permissionName))
    abort(403);

    return response('', 204);



    Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.



    Vue.mixin("can", (permissionName) => 
    let hasAccess;
    axios.get(`/permission/$permissionName`)
    .then(()=>
    hasAccess = true;

    .catch(()=>
    hasAccess = false;
    ;
    return hasAccess;
    );



    And then everytime you wanna check permission, you can just do



    <el-input v-if="can('write-stuff')"> </el-input>





    share|improve this answer



























      3





      +50









      I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.



      Routes:



      Route::get('/permission/permissionName', 'PermissionController@check');


      Controller:



      function check($permissionName) 
      if (! Auth::user()->hasPermissionTo($permissionName))
      abort(403);

      return response('', 204);



      Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.



      Vue.mixin("can", (permissionName) => 
      let hasAccess;
      axios.get(`/permission/$permissionName`)
      .then(()=>
      hasAccess = true;

      .catch(()=>
      hasAccess = false;
      ;
      return hasAccess;
      );



      And then everytime you wanna check permission, you can just do



      <el-input v-if="can('write-stuff')"> </el-input>





      share|improve this answer

























        3





        +50







        3





        +50



        3




        +50





        I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.



        Routes:



        Route::get('/permission/permissionName', 'PermissionController@check');


        Controller:



        function check($permissionName) 
        if (! Auth::user()->hasPermissionTo($permissionName))
        abort(403);

        return response('', 204);



        Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.



        Vue.mixin("can", (permissionName) => 
        let hasAccess;
        axios.get(`/permission/$permissionName`)
        .then(()=>
        hasAccess = true;

        .catch(()=>
        hasAccess = false;
        ;
        return hasAccess;
        );



        And then everytime you wanna check permission, you can just do



        <el-input v-if="can('write-stuff')"> </el-input>





        share|improve this answer













        I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.



        Routes:



        Route::get('/permission/permissionName', 'PermissionController@check');


        Controller:



        function check($permissionName) 
        if (! Auth::user()->hasPermissionTo($permissionName))
        abort(403);

        return response('', 204);



        Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.



        Vue.mixin("can", (permissionName) => 
        let hasAccess;
        axios.get(`/permission/$permissionName`)
        .then(()=>
        hasAccess = true;

        .catch(()=>
        hasAccess = false;
        ;
        return hasAccess;
        );



        And then everytime you wanna check permission, you can just do



        <el-input v-if="can('write-stuff')"> </el-input>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 12 at 17:15









        ShuyiShuyi

        464722




        464722



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312875%2fvue-js-with-laravel-permission%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo