Making WordPress.org


Ignore:
Timestamp:
10/03/2016 02:05:43 PM (8 years ago)
Author:
ocean90
Message:

Translate, Rosetta Roles: Allow import of translations as waiting for plugins and themes.

A PTE/GTE can import translations as waiting for all projects, logged in users only for plugins and themes.

Props akirk, ocean90.
See #1654.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-rosetta-roles/inc/class-plugin.php

    r4083 r4183  
    8484     */
    8585    public function pre_can_user( $verdict, $args ) {
     86        // Logged out users have no permissions.
     87        if ( ! is_user_logged_in() ) {
     88            return false;
     89        }
     90
     91        // No user is allowed to delete something.
    8692        if ( 'delete' === $args['action'] ) {
    8793            return false;
     
    9399        }
    94100
    95         if ( 'approve' !== $args['action'] || ! in_array( $args['object_type'], array( 'project|locale|set-slug', 'translation-set' ) ) ) {
     101        // No permissions for unknown object types.
     102        if ( ! in_array( $args['object_type'], array( 'project|locale|set-slug', 'translation-set' ) ) ) {
    96103            return false;
    97104        }
     
    103110        }
    104111
    105         $locale_slug = $locale_and_project_id->locale;
    106         $current_project_id = $locale_and_project_id->project_id;
     112        // Grant permissions to import plugin/theme translations with status 'waiting'.
     113        if ( 'import-waiting' === $args['action'] ) {
     114            return $this->is_plugin_or_theme_project( $locale_and_project_id->project_id );
     115        }
     116
     117        // The next checks are only for the 'approve' action, no permissions for other actions.
     118        if ( 'approve' !== $args['action'] ) {
     119            return false;
     120        }
    107121
    108122        // Simple check to see if they're an approver or not.
    109         $role = $this->is_approver_for_locale( $args['user_id'], $locale_slug );
     123        $role = $this->is_approver_for_locale( $args['user_id'], $locale_and_project_id->locale );
    110124        if ( ! $role ) {
    111125            return false;
     
    118132
    119133        // Grab the list of Projects (or 'all') that the user can approve.
    120         $project_access_list = $this->get_project_id_access_list( $args['user_id'], $locale_slug );
     134        $project_access_list = $this->get_project_id_access_list( $args['user_id'], $locale_and_project_id->locale );
    121135        if ( ! $project_access_list ) {
    122136            return false;
     
    129143
    130144        // If current project is a parent ID.
    131         if ( in_array( $current_project_id, $project_access_list ) ) {
     145        if ( in_array( $locale_and_project_id->project_id, $project_access_list ) ) {
    132146            return true;
    133147        }
    134148
    135149        // A user is allowed to approve sub projects as well.
    136         $project_access_list = $this->get_project_id_access_list( $args['user_id'], $locale_slug, /* $include_children = */ true );
    137         if ( in_array( $current_project_id, $project_access_list ) ) {
     150        $project_access_list = $this->get_project_id_access_list( $args['user_id'], $locale_and_project_id->locale, /* $include_children = */ true );
     151        if ( in_array( $locale_and_project_id->project_id, $project_access_list ) ) {
    138152            return true;
    139153        }
    140154
    141155        return false;
     156    }
     157
     158    /**
     159     * Determines if the project is for a plugin or theme.
     160     *
     161     * @param int $project_id Project ID.
     162     * @return bool True, if project is a plugin/theme, false if not.
     163     */
     164    public function is_plugin_or_theme_project( $project_id ) {
     165        $project = GP::$project->get( $project_id );
     166        if ( ! $project ) {
     167            return false;
     168        }
     169
     170        return ( 0 === strpos( $project->path, 'wp-plugins/' ) || 0 === strpos( $project->path, 'wp-themes/' ) );
    142171    }
    143172
Note: See TracChangeset for help on using the changeset viewer.