<commit_before><?php

namespace Adldap\Classes;

use Adldap\Collections\AdldapGroupCollection;
use Adldap\Objects\Group;
use Adldap\Adldap;

/**
 * Ldap Group management
 *
 * Class AdldapGroups
 * @package Adldap\classes
 */
class AdldapGroups extends AdldapBase
{
    /**
     * The groups object category string.
     *
     * @var string
     */
    public $objectCategory = 'group';

    /**
     * Returns a complete list of all groups in AD
     *
     * @param bool $includeDescription Whether to return a description
     * @param string $search Search parameters
     * @param bool $sorted Whether to sort the results
     * @return array|bool
     */
    public function all($includeDescription = false, $search = "*", $sorted = true)
    {
        return $this->search(null, $includeDescription, $search, $sorted);
    }

    /**
     * Finds a group and returns it's information.
     *
     * @param string $groupName The group name
     * @param array $fields The group fields to retrieve
     * @return array|bool
     */
    public function find($groupName, $fields = array())
    {
        return $this->adldap->search()
            ->select($fields)
            ->where('objectCategory', '=', $this->objectCategory)
            ->where('anr', '=', $groupName)
            ->first();
    }

    /**
     * Returns a complete list of the groups in AD based on a SAM Account Type
     *
     * @param int $sAMAaccountType The account type to return
     * @param array $select The fields you want to retrieve for each
     * @param bool $sorted Whether to sort the results
     * @return array|bool
     */
    public function search($sAMAaccountType = Adldap::ADLDAP_SECURITY_GLOBAL_GROUP, $select = array(), $sorted = true)
    {
        $this->adldap->utilities()->validateLdapIsBound();

        $search = $this->adldap->search()
            ->select($select)
            ->where('objectCategory', '=', 'group');

        if ($sAMAaccountType !== null)
        {
            $search->where('samaccounttype', '=', $sAMAaccountType);
        }

        if($sorted)
        {
            $search->sortBy('samaccountname', 'asc');
        }

        return $search->get();
    }

    /**
     * Obtain the group's distinguished name based on their group ID
     *
     * @param string $groupName
     * @return string|bool
     */
    public function dn($groupName)
    {
        $group = $this->info($groupName);

        if(is_array($group) && array_key_exists('dn', $group))
        {
            return $group['dn'];
        }

        return false;
    }

    /**
     * Add a group to a group
     *
     * @param string $parent The parent group name
     * @param string $child The child group name
     * @return bool
     */
    public function addGroup($parent,$child)
    {
        // Find the parent group's dn
        $parentGroup = $this->info($parent, array("cn"));

        if ($parentGroup[0]["dn"] === NULL) return false;

        $parentDn = $parentGroup[0]["dn"];

        // Find the child group's dn
        $childGroup = $this->info($child, array("cn"));

        if ($childGroup[0]["dn"] === NULL) return false;

        $childDn = $childGroup[0]["dn"];

        $add = array();
        $add["member"] = $childDn;

        return $this->connection->modAdd($parentDn, $add);
    }

    /**
     * Add a user to a group
     *
     * @param string $group The group to add the user to
     * @param string $user The user to add to the group
     * @param bool $isGUID Is the username passed a GUID or a samAccountName
     * @return bool
     */
    public function addUser($group, $user, $isGUID = false)
    {
        // Adding a user is a bit fiddly, we need to get the full DN of the user<commit_msg>Updated addGroup function<commit_after><?php

namespace Adldap\Classes;

use Adldap\Collections\AdldapGroupCollection;
use Adldap\Objects\Group;
use Adldap\Adldap;

/**
 * Ldap Group management
 *
 * Class AdldapGroups
 * @package Adldap\classes
 */
class AdldapGroups extends AdldapBase
{
    /**
     * The groups object category string.
     *
     * @var string
     */
    public $objectCategory = 'group';

    /**
     * Returns a complete list of all groups in AD
     *
     * @param bool $includeDescription Whether to return a description
     * @param string $search Search parameters
     * @param bool $sorted Whether to sort the results
     * @return array|bool
     */
    public function all($includeDescription = false, $search = "*", $sorted = true)
    {
        return $this->search(null, $includeDescription, $search, $sorted);
    }

    /**
     * Finds a group and returns it's information.
     *
     * @param string $groupName The group name
     * @param array $fields The group fields to retrieve
     * @return array|bool
     */
    public function find($groupName, $fields = array())
    {
        return $this->adldap->search()
            ->select($fields)
            ->where('objectCategory', '=', $this->objectCategory)
            ->where('anr', '=', $groupName)
            ->first();
    }

    /**
     * Returns a complete list of the groups in AD based on a SAM Account Type
     *
     * @param int $sAMAaccountType The account type to return
     * @param array $select The fields you want to retrieve for each
     * @param bool $sorted Whether to sort the results
     * @return array|bool
     */
    public function search($sAMAaccountType = Adldap::ADLDAP_SECURITY_GLOBAL_GROUP, $select = array(), $sorted = true)
    {
        $this->adldap->utilities()->validateLdapIsBound();

        $search = $this->adldap->search()
            ->select($select)
            ->where('objectCategory', '=', 'group');

        if ($sAMAaccountType !== null)
        {
            $search->where('samaccounttype', '=', $sAMAaccountType);
        }

        if($sorted)
        {
            $search->sortBy('samaccountname', 'asc');
        }

        return $search->get();
    }

    /**
     * Obtain the group's distinguished name based on their group ID
     *
     * @param string $groupName
     * @return string|bool
     */
    public function dn($groupName)
    {
        $group = $this->info($groupName);

        if(is_array($group) && array_key_exists('dn', $group))
        {
            return $group['dn'];
        }

        return false;
    }

    /**
     * Add a group to a group
     *
     * @param string $parent The parent group name
     * @param string $child The child group name
     * @return bool
     */
    public function addGroup($parent,$child)
    {
        // Find the parent group's dn
        $parentGroup = $this->find($parent);

        if ($parentGroup["dn"] === NULL) return false;

        $parentDn = $parentGroup["dn"];

        // Find the child group's dn
        $childGroup = $this->info($child);

        if ($childGroup["dn"] === NULL) return false;

        $childDn = $childGroup["dn"];

        $add = array();
        $add["member"] = $childDn;

        return $this->connection->modAdd($parentDn, $add);
    }

    /**
     * Add a user to a group
     *
     * @param string $group The group to add the user to
     * @param string $user The user to add to the group
     * @param bool $isGUID Is the username passed a GUID or a samAccountName
     * @return bool
     */
    public function addUser($group, $user, $isGUID = false)
    {
        // Adding a user is a bit fiddly, we need to get the full DN of the user
{'fraction_non_alphanumeric': 0.12741416309012876, 'fraction_numerical': 0.000536480686695279, 'mean_word_length': 2.2295365959289737, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3813334', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2161, 'n_words': 659}