protected function check_usercreationrestriction()

in classes/feature/usersync/main.php [700:771]


    protected function check_usercreationrestriction($aaddata) {
        $restriction = get_config('local_o365', 'usersynccreationrestriction');
        if (empty($restriction)) {
            return true;
        }
        $restriction = @unserialize($restriction);
        if (empty($restriction) || !is_array($restriction)) {
            return true;
        }
        if (empty($restriction['remotefield']) || empty($restriction['value'])) {
            return true;
        }
        $useregex = (!empty($restriction['useregex'])) ? true : false;

        if ($restriction['remotefield'] === 'o365group') {
            if (unified::is_configured() !== true) {
                utils::debug('graph api is not configured.', 'check_usercreationrestriction');
                return false;
            }

            $apiclient = $this->construct_user_api();

            try {
                $group = $apiclient->get_group_by_name($restriction['value']);
                if (empty($group) || !isset($group['id'])) {
                    utils::debug('Could not find group (1)', 'check_usercreationrestriction', $group);
                    return false;
                }
                $usergroupsresults = $apiclient->get_user_transitive_groups($aaddata['id']);
                $usergroups = $usergroupsresults['value'];
                while (!empty($usergroupsresults['@odata.nextLink'])) {
                    $nextlink = parse_url($usergroupsresults['@odata.nextLink']);
                    if (isset($nextlink['query'])) {
                        $query = [];
                        parse_str($nextlink['query'], $query);
                        if (isset($query['$skiptoken'])) {
                            $usergroupsresults = $apiclient->get_user_transitive_groups($aaddata['id']);
                            $usergroups = array_merge($usergroups, $usergroupsresults['value']);
                        }
                    }
                }

                foreach ($usergroups as $usergroup) {
                    if ($group['id'] === $usergroup) {
                        return true;
                    }
                }
                return false;
            } catch (\Exception $e) {
                utils::debug('Could not find group (2)', 'check_usercreationrestriction', $e);
                return false;
            }
        } else {
            if (!isset($aaddata[$restriction['remotefield']])) {
                return false;
            }
            $fieldval = $aaddata[$restriction['remotefield']];
            $restrictionval = $restriction['value'];

            if ($useregex === true) {
                $count = @preg_match('/'.$restrictionval.'/', $fieldval, $matches);
                if (!empty($count)) {
                    return true;
                }
            } else {
                if ($fieldval === $restrictionval) {
                    return true;
                }
            }
        }
        return false;
    }