Making WordPress.org


Ignore:
Timestamp:
10/13/2020 01:50:10 AM (6 years ago)
Author:
dd32
Message:

Trac: API: Add an internal API to rename a user on a Trac instance (ie. johnsmith to johndoe) and an anonymization method for GDPR purposes (eg. johnsmith to Anonymous 123).

Work in progress.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-notifications-db.php

    r10365 r10367  
    227227                );
    228228        }
     229
     230        /**
     231         * Anonymize a user on Trac.
     232         *  - Switches their tickets, comments, and attachments to be owned by $to
     233         *  - Removes Subscriptions & notification prefs
     234         *  - Removes user Trac preferences
     235         *
     236         * @param string $from The user login of the user to anonymize.
     237         * @param string $to   The new user login placeholder for the user, must be unique.
     238         */
     239        function anonymize_user( $from, $to ) {
     240                $from = trim( $from );
     241                $to   = trim( $to );
     242
     243                if ( empty( $from ) || empty( $to ) ) {
     244                        return false;
     245                }
     246
     247                // Perform rename
     248                if ( ! $this->rename_user( $from, $to ) ) {
     249                        return false;
     250                }
     251
     252                // Remove Trac sessions & preferences
     253                $this->db->delete( 'session', array( 'sid' => $from ) );
     254                $this->db->delete( 'session', array( 'sid' => $to ) );
     255                $this->db->delete( 'session_attribute', array( 'sid' => $from ) );
     256                $this->db->delete( 'session_attribute', array( 'sid' => $to ) );
     257
     258                // Remove Authentication cookies (May not be applicable to WordPress.org trac)
     259                $this->db->delete( 'auth_cookie', array( 'name' => $from ) );
     260                $this->db->delete( 'auth_cookie', array( 'name' => $to ) );
     261
     262                // Remove any Trac notification & subscription settings (May not be applicable to WordPress.org trac)
     263                $this->db->delete( 'notify_watch', array( 'sid' => $from ) );
     264                $this->db->delete( 'notify_watch', array( 'sid' => $to ) );
     265                $this->db->delete( 'notify_subscription', array( 'sid' => $from ) );
     266                $this->db->delete( 'notify_subscription', array( 'sid' => $to ) );
     267
     268                // Remove subscriptions and notifications (Should all be owned by $to, but do $from just in case)
     269                $this->db->delete( '_ticket_subs',   array( 'username' => $from ) );
     270                $this->db->delete( '_ticket_subs',   array( 'username' => $to ) );
     271                $this->db->delete( '_notifications', array( 'username' => $from ) );
     272                $this->db->delete( '_notifications', array( 'username' => $to ) );
     273
     274                return true;
     275        }
     276
     277        /**
     278         * Rename a user on Trac, can be used for username migrations and GDPR anonymization needs.
     279         *
     280         * @param string $from The user login of the user to rename.
     281         * @param string $to   The new user login that the items owned by $from will be reauthored to.
     282         */
     283        function rename_user( $from, $to ) {
     284                $from = trim( $from );
     285                $to   = trim( $to );
     286
     287                // Prevent data issues by ensuring that both are supplied.
     288                if ( empty( $from ) || empty( $to ) ) {
     289                        return false;
     290                }
     291
     292                // If the user has (or will have) specific permissions on the trac instance, bail.
     293                if ( $this->db->get_var( $wpdb->prepare(
     294                        "SELECT action FROM permission WHERE username IN( %s, %s )",
     295                        $from,
     296                        $to
     297                ) ) ) {
     298                        return false;
     299                }
     300
     301                // Trac Sessions & Prefs
     302                $this->db->get_var( $this->db->prepare(
     303                        "UPDATE session SET sid = %s WHERE sid = %s",
     304                        $to,
     305                        $from
     306                ) );
     307                $this->db->get_var( $this->db->prepare(
     308                        "UPDATE auth_cookie SET name = %s WHERE name = %s",
     309                        $to,
     310                        $from
     311                ) );
     312                $this->db->get_var( $this->db->prepare(
     313                        "UPDATE session_attribute SET sid = %s WHERE sid = %s",
     314                        $to,
     315                        $from
     316                ) );
     317
     318                // Tickets, Attachments, and Comments.
     319                $this->db->get_var( $this->db->prepare(
     320                        "UPDATE ticket SET reporter = %s WHERE reporter = %s",
     321                        $to,
     322                        $from
     323                ) );
     324
     325                $this->db->get_var( $this->db->prepare(
     326                        "UPDATE ticket SET owner = %s WHERE owner = %s",
     327                        $to,
     328                        $from
     329                ) );
     330
     331                $this->db->get_var( $this->db->prepare(
     332                        "UPDATE attachment SET author = %s WHERE author = %s",
     333                        $to,
     334                        $from
     335                ) );
     336
     337                $this->db->get_var( $this->db->prepare(
     338                        "UPDATE ticket_change SET author = %s WHERE author = %s",
     339                        $to,
     340                        $from
     341                ) );
     342       
     343                $this->db->get_var( $this->db->prepare(
     344                        "UPDATE wiki SET author = %s WHERE author = %s",
     345                        $to,
     346                        $from
     347                ) );
     348
     349                // WordPress.org Subscriptions and notifications.
     350                $this->db->get_var( $this->db->prepare(
     351                        "UPDATE _ticket_subs SET username = %s WHERE username = %s",
     352                        $to,
     353                        $from
     354                ) );
     355
     356                $this->db->get_var( $this->db->prepare(
     357                        "UPDATE _notifications SET username = %s WHERE username = %s",
     358                        $to,
     359                        $from
     360                ) );
     361
     362                // DO NOT update the following tables:
     363                // - revision (SVN "cache")
     364                // - permission (Trac permissions)
     365
     366                return true;
     367        }
    229368}
Note: See TracChangeset for help on using the changeset viewer.