Create a user and assign roles to it.

@param array $data
@param array $roles

@return EloquentUser
165404
public function createWithRoles(array $data, $roles)
    {
        $user = $this->create((array) $data);

        if (! empty($roles)) {
            $user->roles()->attach($roles);
        }

        return $user;
    }
1000521
protected function create(array $data)
    {
        $fields = [
            'name'     => $data['name'],
            'email'    => $data['email'],
            'password' => bcrypt($data['password']),
        ];
        if (config('auth.providers.users.field', 'email') === 'username' && isset($data['username'])) {
            $fields['username'] = $data['username'];
        }
        return User::create($fields);
    }
186196
final protected function addRoles(object $data, array $roles): BoundedRoleInterface
    {
        foreach ($roles as $role) {
            $data = $this->addRole($data, $role);
        }

        return $data;
    }
1284197
public static function assign_roles($assignments) {
        global $DB;

        // Do basic automatic PARAM checks on incoming data, using params description
        // If any problems are found then exceptions are thrown with helpful error messages
        $params = self::validate_parameters(self::assign_roles_parameters(), array('assignments'=>$assignments));

        $transaction = $DB->start_delegated_transaction();

        foreach ($params['assignments'] as $assignment) {
            // Ensure correct context level with a instance id or contextid is passed.
            $context = self::get_context_from_params($assignment);

            // Ensure the current user is allowed to run this function in the enrolment context.
            self::validate_context($context);
            require_capability('moodle/role:assign', $context);

            // throw an exception if user is not able to assign the role in this context
            $roles = get_assignable_roles($context, ROLENAME_SHORT);

            if (!array_key_exists($assignment['roleid'], $roles)) {
                throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
            }

            role_assign($assignment['roleid'], $assignment['userid'], $context->id);
        }

        $transaction->allow_commit();
    }
39604
public function addRole($id, $role)
    {
        if (empty($role) || !$user = $this->getUser($id)) {
            return false;
        }

        // Add the role to the $user['roles'] array
        $user['roles'][] = $role;

        return $this->saveUser($user);
    }
1277269
public function find($username, array $data = [])
    {
        $response = $this->request->get(sprintf("users/%s", $username), $data);
        return new User($this->master, $response);
    }
666281
protected function create(array $data)
    {
        $admin = new Admin();

        $fields           = $this->tableFields();
        $data['password'] = bcrypt($data['password']);
        foreach ($fields as $field) {
            if (isset($data[$field])) {
                $admin->$field = $data[$field];
            }
        }

        $admin->save();
        $admin->roles()->sync(request('role_id'));
        $this->sendConfirmationNotification($admin, request('password'));

        return $admin;
    }
115759
public function createRoleAssignment(
        ?RoleAssignmentCreateData $data = null,
        ?string $name = null
    ): FormInterface {
        $name = $name ?: StringUtil::fqcnToBlockPrefix(RoleAssignmentCreateType::class);

        return $this->formFactory->createNamed(
            $name,
            RoleAssignmentCreateType::class,
            $data ?? new RoleAssignmentCreateData()
        );
    }
236973
public function mapRoles(array $data)
    {
        $roleData = array();
        foreach ($data as $row) {
            $roleData[$row['ezrole_id']][] = $row;
        }

        $roles = array();
        foreach ($roleData as $data) {
            $roles[] = $this->mapRole($data);
        }

        return $roles;
    }
599973
public function createRole(array $data = [])
    {
        $deferred = new Deferred();

        $rolePart = $this->factory->create(Role::class);

        $this->roles->save($rolePart)->then(
            function ($role) use ($deferred, $data) {
                $role->fill($data);

                $this->roles->save($role)->then(
                    function ($role) use ($deferred) {
                        $deferred->resolve($role);
                    },
                    \React\Partial\bind_right($this->reject, $deferred)
                );
            },
            \React\Partial\bind_right($this->reject, $deferred)
        );

        return $deferred->promise();
    }
317124
public function create($data)
    {
        $data = $this->retrieveData($data);
        $insertData = [
            'role_id'=> $this->getRoleId(),
            'resource' => $data['resource'],
            'methods'=> implode(',', $data['methods']),
        ];
        $this->table->insert($insertData);

        return $data;
    }
625957
protected function _addRoles($roles)
    {
        foreach($roles as $role) {
            if (!$this->hasRole($role->getName())) {
                $parents = $role->getParents()->toArray();
                $parentNames = array();
                foreach($parents as $parent) {
                    $parentNames[] = $parent->getName();
                }
                $this->addRole(new Role($role->getName()), $parentNames);
            }
        }

        return $this;
    }
660295
protected function setPodiumUserRole($user, $role)
    {
        $rbac = Podium::getInstance()->getRbac();
        $userRoles = $rbac->getRolesByUser($user->id);
        $podiumRoles = array_filter($userRoles, function ($role) {
            return strpos($role->name, 'podium') === 0;
        });
        foreach ($podiumRoles as $podiumRole) {
            $rbac->revoke($podiumRole, $user->id);
        }
        $rbac->assign($role, $user->id);
    }
1224586
protected function createPermissionObject($resource, $context, $action, $roles) {
    $permission = $this->persistenceFacade->create($this->permissionType);
    $permission->setValue('resource', $resource);
    $permission->setValue('context', $context);
    $permission->setValue('action', $action);
    $permission->setValue('roles', $roles);
    return $permission;
  }
927346
public function generateSocialOnlyUser($email, $roles)
    {
        $user = static::firstOrCreate(compact('email'));

        $user->setStatus(Status::ACTIVE);
        $user->assignRole($roles);
        $user->trackTime('invited_at');

        Detail::firstOrCreate([
            'user_id'      => $user->id,
            'display_name' => $email,
        ]);

        return $user;
    }
525191
public function getAccessibleRoles(array $roles)
    {
        $accessibleRoles = $roles;
        foreach ($roles as $role) {
            /**
             * @var $role Role
             */
            if (isset($this->map[$role->getRole()])) {
                foreach ($this->map[$role->getRole()] as $r) {
                    $accessibleRoles[] = new Role($r);
                }
            }
        }

        return $accessibleRoles;
    }
1009795
public function createUserFromOAuth($data)
    {
        $user = $this->createUser();
        $user->setAuthDataByProvider($data['provider'], ['id' => $data['id'], 'token' => $data['token']]);
        $user->setAuthProvider($data['provider']);
        $user->setFirstAuthProvider($data['provider']);
        $user->setUsername($data['email']);
        $user->setEmail($data['email']);
        $user->setPassword('none');
        $user->setEnabled(true);
        $user->setContact(new Contact);

        return $user;
    }