Editor - Permissions to create, edit or delete records

Editor - Permissions to create, edit or delete records

peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
edited November 2019 in Editor

Is there a way to set user permissions to create, edit or delete records and just have a read only permission?

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @peterbrowne ,

    This page of the manual should help, it discusses that.

    Cheers,

    Colin

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    I need to set permissions at the highest level for all datatables instances, not for individual fields.

    So a person with read only permission will not see any Editor buttons for any tables.

    A person with edit function will only see the Edit button.

    An admin or super user will have access to all editor buttons for all tables.

    I would have thought this would be a fundamental need...

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    So on the $_SESSION global there may be:

    $_SESSION['cm_user'] = Array ( [cm_user] => Array ( [user] => '123456' [permission] => 'edit' ) [LAST_ACTIVITY] => 1573091380 )

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    You are right - it is a fairly fundamental need in a CRUD system. However, Editor makes no assumptions about the authentication / session framework you are using, so a little bit of code is required.

    A person with edit function will only see the Edit button.

    That information needs to be given to the client-side, since it is the client-side that is rendering the buttons.

    So you could do something like:

    if (user.permissions.create) {
      table.buttons().add({
        extend: 'create',
        editor: editor
      });
    }
    
    if (user.permissions.edit) {
      table.buttons().add({
        extend: 'edit',
        editor: editor
      });
    }
    
    // ...
    

    Typically I have a little bit of PHP generating Javascript to get that kind of information from the PHP session into Javascript.

    Allan

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
    edited November 2019

    Thanks Allan.

    How should I modify the DataTables initialisation to get this to work? I have tried removing the buttons part of the code, but it breaks the DataTable.

        if ( permision == 'create' || permision == 'super' ) {
                    $( '#year_table' ).DataTable().buttons().add( {
                        extend: 'create',
                        editor: editor
                    } );
                }
    
                if ( permision == 'edit' || permision == 'super' ) {
                    $( '#year_table' ).DataTable().buttons().add( {
                        extend: 'edit',
                        editor: editor
                    } );
                }
    
                if ( permision == 'super' ) {
                    $( '#year_table' ).DataTable().buttons().add( {
                        extend: 'delete',
                        editor: editor
                    } );
                }
    
    
                $( '#year_table' ).DataTable( {
                    responsive: true,
                    ajax: "program_data/year_data.php",
                    dom: "Bfrtip",
                    columns: [ {
                        data: "year_name"
                    }, {
                        data: "modified"
                    }, {
                        data: "modified_by"
                    } ],
                    select: {
                        style: 'os',
                        selector: 'td:first-child'
                    },
                    buttons: [ {
                        extend: "create",
                        editor: editor
                    }, {
                        extend: "edit",
                        editor: editor
                    }, {
                        extend: "remove",
                        editor: editor
                    } ]
                } );
    
  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
    edited November 2019

    Also using the code below prduces an error:

    DataTables warning: table id=year_table - Cannot reinitialise DataTable.

    $( '#year_table' ).DataTable().button().add( {
                            extend: 'delete',
                            editor: editor
                        } );
    
    
                    $( '#year_table' ).DataTable( {
                        responsive: true,
                        ajax: "program_data/year_data.php",
                        dom: "Bfrtip",
                        columns: [ {
                            data: "year_name"
                        }, {
                            data: "modified"
                        }, {
                            data: "modified_by"
                        } ],
                        select: {
                            style: 'os',
                            selector: 'td:first-child'
                        },
                        buttons: [ ]
                    } );
    
  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
    edited November 2019

    Also tried:

    var table = $( '#year_table' ).DataTable( {
                responsive: true,
                ajax: "program_data/year_data.php",
                dom: "Bfrtip",
                columns: [ {
                    data: "year_name"
                }, {
                    data: "modified"
                }, {
                    data: "modified_by"
                } ],
                select: {
                    style: 'os',
                    selector: 'td:first-child'
                },
                buttons: [ ]
            } );
    
                        table.buttons().add( {
                extend: 'create',
                editor: editor
            } );
    

    But datatable does not have any buttons

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Apologies - the function should have been button().add() (not the plural buttons()) and I'd forgotten about the index for the button as well:

    So it should be:

    table.button().add(null, {extend: 'create', editor: editor})
    

    Here is a little example: http://live.datatables.net/garofaba/1/edit (using the print button).

    Allan

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    And here it is with Editor's buttons: http://live.datatables.net/lucidiba/2/edit

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    Brilliant! Works great.

        if ( permission == 'edit' || permission == 'super' ) {
                    table.button().add( null, {
                        extend: 'edit',
                        editor: editor
                    } );
                }
    

    Thanks

This discussion has been closed.