src/Helpers/ProcessHelper.php
public static function changeScriptOwner($user, $group = '')
    {
        $uInfo = posix_getpwnam($user);

        if (!$uInfo || !isset($uInfo['uid'])) {
            throw new \RuntimeException("User ({$user}) not found.");
        }

        $uid = (int)$uInfo['uid'];

        // Get gid.
        if ($group) {
            if (!$gInfo = posix_getgrnam($group)) {
                throw new \RuntimeException("Group {$group} not exists", -300);
            }

            $gid = (int)$gInfo['gid'];
        } else {
            $gid = (int)$uInfo['gid'];
        }

        if (!posix_initgroups($uInfo['name'], $gid)) {
            throw new \RuntimeException("The user [{$user}] is not in the user group ID [GID:{$gid}]", -300);
        }

        posix_setgid($gid);

        if (posix_geteuid() !== $gid) {
            throw new \RuntimeException("Unable to change group to {$user} (UID: {$gid}).", -300);
        }

        posix_setuid($uid);

        if (posix_geteuid() !== $uid) {
            throw new \RuntimeException("Unable to change user to {$user} (UID: {$uid}).", -300);
        }
    }
public static function changeScriptOwner($user, $group = '')
    {
        $uInfo = posix_getpwnam($user);

        if (!$uInfo || !isset($uInfo['uid'])) {
            throw new \RuntimeException("User ({$user}) not found.");
        }

        $uid = (int)$uInfo['uid'];

        // Get gid.
        if ($group) {
            if (!$gInfo = posix_getgrnam($group)) {
                throw new \RuntimeException("Group {$group} not exists", -300);
            }

            $gid = (int)$gInfo['gid'];
        } else {
            $gid = (int)$uInfo['gid'];
        }

        if (!posix_initgroups($uInfo['name'], $gid)) {
            throw new \RuntimeException("The user [{$user}] is not in the user group ID [GID:{$gid}]", -300);
        }

        posix_setgid($gid);

        if (posix_geteuid() !== $gid) {
            throw new \RuntimeException("Unable to change group to {$user} (UID: {$gid}).", -300);
        }

        posix_setuid($uid);

        if (posix_geteuid() !== $uid) {
            throw new \RuntimeException("Unable to change user to {$user} (UID: {$uid}).", -300);
        }
    }
public static function changeScriptOwner ( $ user , $ group = '' ) { $ uInfo = posix_getpwnam ( $ user ) ; if ( ! $ uInfo || ! isset ( $ uInfo [ 'uid' ] ) ) { throw new \ RuntimeException ( "User ({$user}) not found." ) ; } $ uid = ( int ) $ uInfo [ 'uid' ] ; // Get gid. if ( $ group ) { if ( ! $ gInfo = posix_getgrnam ( $ group ) ) { throw new \ RuntimeException ( "Group {$group} not exists" , - 300 ) ; } $ gid = ( int ) $ gInfo [ 'gid' ] ; } else { $ gid = ( int ) $ uInfo [ 'gid' ] ; } if ( ! posix_initgroups ( $ uInfo [ 'name' ] , $ gid ) ) { throw new \ RuntimeException ( "The user [{$user}] is not in the user group ID [GID:{$gid}]" , - 300 ) ; } posix_setgid ( $ gid ) ; if ( posix_geteuid ( ) !== $ gid ) { throw new \ RuntimeException ( "Unable to change group to {$user} (UID: {$gid})." , - 300 ) ; } posix_setuid ( $ uid ) ; if ( posix_geteuid ( ) !== $ uid ) { throw new \ RuntimeException ( "Unable to change user to {$user} (UID: {$uid})." , - 300 ) ; } }
Set unix user and group for current process script.
@param string $user
@param string $group
@throws \RuntimeException
Set unix user and group for current process script .