Mojolicious-Plugin-Fondation-Perm-UI-Bootstrap

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


    Two functions:

    - `loadPerms(group)`

        Fetches all permissions via `GET /api/perm`, renders checkboxes, and pre-checks
        those the group already has (from `group.perms`).

    - `collectPermAssignments()`

        Returns an array of checked permission IDs — called by `validateGroupForm()`
        in `DatatableGroup.js` before save.

# AUTHOR

Daniel Brosseau <dab@cpan.org>

# COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Daniel Brosseau.

lib/Mojolicious/Plugin/Fondation/Perm/UI/Bootstrap.pm  view on Meta::CPAN


=over

=item C<loadPerms(group)>

Fetches all permissions via C<GET /api/perm>, renders checkboxes, and pre-checks
those the group already has (from C<group.perms>).

=item C<collectPermAssignments()>

Returns an array of checked permission IDs — called by C<validateGroupForm()>
in C<DatatableGroup.js> before save.

=back

=back

=head1 AUTHOR

Daniel Brosseau <dab@cpan.org>

share/public/js/DatatablePerm.js  view on Meta::CPAN

$(function() {
    if (!$('#panel-perm').length) return;
    initialiseTabPerm();
    $('#menuAddPerm').on('click', function(event) {
        addPerm();
    });

    // ────────────────────────────────────────────────
    // Form validation using validators.js
    // ────────────────────────────────────────────────
    async function validatePermForm() {
        const form = document.getElementById('perm-form');
        if (!form) {
            console.error("Form #perm-form not found");
            return { valid: false, errors: [{ field: null, message: l("Form not found") }], data: {} };
        }

        const formDataObj = Object.fromEntries(new FormData(form).entries());

        // Cleanup (trim all strings)
        Object.keys(formDataObj).forEach(key => {
            if (typeof formDataObj[key] === 'string') {
                formDataObj[key] = formDataObj[key].trim();
            }
        });

        // Validate via FondationValidators
        if (typeof window.FondationValidators === 'undefined' || typeof FondationValidators.validate !== 'function') {
            console.error("FondationValidators not loaded or invalid");
            return { valid: false, errors: [{ field: null, message: l("Validator not available") }], data: {} };
        }

        const schemaName = 'Perm';

        const result = FondationValidators.validate(schemaName, formDataObj);

        return {
            valid: result.valid,
            errors: result.errors.map(msg => ({
                field: null,
                message: msg
            })),
            data: formDataObj
        };
    }

share/public/js/DatatablePerm.js  view on Meta::CPAN

    }

    // ────────────────────────────────────────────────
    // "Save" button
    // ────────────────────────────────────────────────
    $('#saveObject').on('click', async function() {
        const button = $(this);
        button.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> ' + l("Saving..."));

        try {
            const validation = await validatePermForm();
            if (!validation.valid) {
                displayValidationErrors(validation.errors);
                return;
            }

            const id = validation.data.id;
            const method = id ? 'PUT' : 'POST';
            const url = id ? `/api/perm/${id}` : '/api/perm';

            // id is in the URL path, not the request body

share/templates/zones/js/group/add/perms.js.ep  view on Meta::CPAN

            sel.append('<option value="' + p.id + '"' + selected + '>' + p.name + '</option>');
        }

        sel.selectpicker('refresh');
        $('#perms-group').show();
    });
}

/**
 * Intercepts the save to collect permission assignments.
 * Called by validateGroupForm() in DatatableGroup.js before save.
 */
function collectPermAssignments() {
    return $('#perms').val() || [];
}
</script>



( run in 1.183 second using v1.01-cache-2.11-cpan-ad19def0cd9 )