<commit_before><?php

/**
 * DNEnvironment
 *
 * This dataobject represents a target environment that source code can be
 * deployed to.
 *
 */
class DNEnvironment extends DataObject {

	/**
	 * If this is set to a full pathfile, it will be used as template
	 * file when creating a new capistrano environment config file.
	 * 
	 * If not set, the default 'environment.template' from the module 
	 * root is used
	 *
	 * @var string
	 */
	private static $template_file = '';
	
	/**
	 * Set this to true to allow editing of the environment files via the web admin
	 *
	 * @var bool
	 */
	private static $allow_web_editing = false;
	
	/**
	 *
	 * @var array
	 */
	public static $db = array(
		"Filename" => "Varchar(255)",
		"Name" => "Varchar",
		"URL" => "Varchar",
		"GraphiteServers" => "Text",
	);

	/**
	 *
	 * @var array
	 */
	public static $has_one = array(
		"Project" => "DNProject",
	);

	/**
	 *
	 * @var array
	 */
	public static $has_many = array(
		"DataArchives" => "DNDataArchive",
	);

	/**
	 *
	 * @var array
	 */
	public static $many_many = array(
		"Deployers" => "Member",
		"ArchiveUploaders" => "Member", // Who can upload archive files to this environment
		"ArchiveDownloaders" => "Member" // Who can download archive files from this environment
	);

	/**
	 *
	 * @var array
	 */
	public static $summary_fields = array(
		"Name",
		"URL",
		"DeployersList",
		"ArchiveUploadersList",
		"ArchiveDownloadersList"
	);

	/**
	 *
	 * @var array
	 */
	public static $searchable_fields = array(
		"Name",
	);
	
	/**
	 *
	 * @var string 
	 */
	private static $default_sort = 'Name';

	/**
	 * Caches the relation to the Parent Project
	 *
	 * @var array
	 */
	protected static $relation_cache = array();

	/**
	 * 
	 * @todo this should probably be refactored so it don't interfere with the default
	 * DataObject::get() behaviour.
	 *
	 * @param string $callerClass
	 * @param string $filter
	 * @param string $sort
	 * @param string $join
	 * @param string $limit
	 * @param string $containerClass
	 * @return \DNEnvironmentList
	 */
	public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null,
			$containerClass = 'DataList') {
		return new DNEnvironmentList('DNEnvironment');
	}

	/**
	 * Used by the sync task
	 *
	 * @param string $path
	 * @return \DNEnvironment
	 */
	public static function create_from_path($path) {
		$e = new DNEnvironment;
		$e->Filename = $path;
		$e->Name = basename($e->Filename, '.rb');

		// add each administrator member as a deployer of the new environment
		$adminGroup = Group::get()->filter('Code', 'administrators')->first();
		if($adminGroup && $adminGroup->exists()) {
			foreach($adminGroup->Members() as $member) {
				$e->Deployers()->add($member);
			}
		}
		return $e;
	}

	/**
	 * @return string
	 */
	public function getFullName() {
		return $this->Project()->Name . ':' . $this->Name;
	}

	/**
	 *
	 * @return DNProject
	 */
	public function Project() {
		if(!isset(self::$relation_cache['DNProject.' . $this->ProjectID])) {
			self::$relation_cache['DNProject.' . $this->ProjectID] = $this->getComponent('Project');
		}
		return self::$relation_cache['DNProject.' . $this->ProjectID];
	}

	/**
	 * Environments are only viewable by people that can view the parent project
	 *
	 * @param Member $member
	 * @return boolean
	 */
	public function canView($member = null) {
		return $this->Project()->canView($member);
	}

	/**
	 * Allow deploy only to some people.
	 *
	 * @param Member $member
	 * @return boolean
	 */
	public function canDeploy($member = null) {
		if(!$member) $member = Member::currentUser();

		return (bool)($this->Deployers()->byID($member->ID));
	}

	/**
	 * Allow only selected {@link Member} objects to upload archives to this {@link DNEnvironment}.
	 *
	 * @param Member $member
	 * @return boolean true if $member can upload archives to this environment
	 */
	public function canUploadArchive($member = null) {
		if(!$member) $member = Member::currentUser();

		return (bool)($this->ArchiveUploaders()->byID($member->ID));
	}

	/**
	 * Allow only selected {@link Member} objects to download archives from this {@link DNEnvironment}.
	 *
	 * @param Member $member
	 * @return boolean true if $member can download archives from this environment
	 */
	public function canDownloadArchive($member = null) {
		if(!$member) $member = Member::currentUser();

		return (bool)($this->ArchiveDownloaders()->byID($member->ID));
	}

	/**
	 * Get a string of people that are allowed to deploy to this environment.
	 * Used in DNRoot_project.ss to list {@link Member}s who has permission to perform this action.
	 *
	 * @return string
	 */
	public function getDeployersList() {
		return implode(", ", $this->Deployers()->column("FirstName"));
	}

	/**
	 * Get a string of people that are allowed to upload archives to this environment.
	 * Used in DNRoot_project.ss to list {@link Member}s who has permission to perform this action.
	 *
	 * @return string
	 */
	public function getArchiveUploadersList() {<commit_msg>Fix permission failure when permission checks are called without Member.<commit_after><?php

/**
 * DNEnvironment
 *
 * This dataobject represents a target environment that source code can be
 * deployed to.
 *
 */
class DNEnvironment extends DataObject {

	/**
	 * If this is set to a full pathfile, it will be used as template
	 * file when creating a new capistrano environment config file.
	 * 
	 * If not set, the default 'environment.template' from the module 
	 * root is used
	 *
	 * @var string
	 */
	private static $template_file = '';
	
	/**
	 * Set this to true to allow editing of the environment files via the web admin
	 *
	 * @var bool
	 */
	private static $allow_web_editing = false;
	
	/**
	 *
	 * @var array
	 */
	public static $db = array(
		"Filename" => "Varchar(255)",
		"Name" => "Varchar",
		"URL" => "Varchar",
		"GraphiteServers" => "Text",
	);

	/**
	 *
	 * @var array
	 */
	public static $has_one = array(
		"Project" => "DNProject",
	);

	/**
	 *
	 * @var array
	 */
	public static $has_many = array(
		"DataArchives" => "DNDataArchive",
	);

	/**
	 *
	 * @var array
	 */
	public static $many_many = array(
		"Deployers" => "Member",
		"ArchiveUploaders" => "Member", // Who can upload archive files to this environment
		"ArchiveDownloaders" => "Member" // Who can download archive files from this environment
	);

	/**
	 *
	 * @var array
	 */
	public static $summary_fields = array(
		"Name",
		"URL",
		"DeployersList",
		"ArchiveUploadersList",
		"ArchiveDownloadersList"
	);

	/**
	 *
	 * @var array
	 */
	public static $searchable_fields = array(
		"Name",
	);
	
	/**
	 *
	 * @var string 
	 */
	private static $default_sort = 'Name';

	/**
	 * Caches the relation to the Parent Project
	 *
	 * @var array
	 */
	protected static $relation_cache = array();

	/**
	 * 
	 * @todo this should probably be refactored so it don't interfere with the default
	 * DataObject::get() behaviour.
	 *
	 * @param string $callerClass
	 * @param string $filter
	 * @param string $sort
	 * @param string $join
	 * @param string $limit
	 * @param string $containerClass
	 * @return \DNEnvironmentList
	 */
	public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null,
			$containerClass = 'DataList') {
		return new DNEnvironmentList('DNEnvironment');
	}

	/**
	 * Used by the sync task
	 *
	 * @param string $path
	 * @return \DNEnvironment
	 */
	public static function create_from_path($path) {
		$e = new DNEnvironment;
		$e->Filename = $path;
		$e->Name = basename($e->Filename, '.rb');

		// add each administrator member as a deployer of the new environment
		$adminGroup = Group::get()->filter('Code', 'administrators')->first();
		if($adminGroup && $adminGroup->exists()) {
			foreach($adminGroup->Members() as $member) {
				$e->Deployers()->add($member);
			}
		}
		return $e;
	}

	/**
	 * @return string
	 */
	public function getFullName() {
		return $this->Project()->Name . ':' . $this->Name;
	}

	/**
	 *
	 * @return DNProject
	 */
	public function Project() {
		if(!isset(self::$relation_cache['DNProject.' . $this->ProjectID])) {
			self::$relation_cache['DNProject.' . $this->ProjectID] = $this->getComponent('Project');
		}
		return self::$relation_cache['DNProject.' . $this->ProjectID];
	}

	/**
	 * Environments are only viewable by people that can view the parent project
	 *
	 * @param Member $member
	 * @return boolean
	 */
	public function canView($member = null) {
		return $this->Project()->canView($member);
	}

	/**
	 * Allow deploy only to some people.
	 *
	 * @param Member $member
	 * @return boolean
	 */
	public function canDeploy($member = null) {
		if(!$member) $member = Member::currentUser();
		if(!$member) return false; // Must be logged in to check permissions

		return (bool)($this->Deployers()->byID($member->ID));
	}

	/**
	 * Allow only selected {@link Member} objects to upload archives to this {@link DNEnvironment}.
	 *
	 * @param Member $member
	 * @return boolean true if $member can upload archives to this environment
	 */
	public function canUploadArchive($member = null) {
		if(!$member) $member = Member::currentUser();
		if(!$member) return false; // Must be logged in to check permissions

		return (bool)($this->ArchiveUploaders()->byID($member->ID));
	}

	/**
	 * Allow only selected {@link Member} objects to download archives from this {@link DNEnvironment}.
	 *
	 * @param Member $member
	 * @return boolean true if $member can download archives from this environment
	 */
	public function canDownloadArchive($member = null) {
		if(!$member) $member = Member::currentUser();
		if(!$member) return false; // Must be logged in to check permissions

		return (bool)($this->ArchiveDownloaders()->byID($member->ID));
	}

	/**
	 * Get a string of people that are allowed to deploy to this environment.
	 * Used in DNRoot_project.ss to list {@link Member}s who has permission to perform this action.
	 *
	 * @return string
	 */
	public function getDeployersList() {
		return implode(", ", $this->Deployers()->column("FirstName"));
	}

	/**
	 * Get a string of people that are allowed to upload archives to this environment.
	 * Used in DNRoot_project.ss to list {@link Member}s who has permission to perform this action.
	 *
	 * @return string
	 */
	public function getArchiveUploadersList() {
{'fraction_non_alphanumeric': 0.14619996132276156, 'fraction_numerical': 0.0005801585766776252, 'mean_word_length': 3.4581896551724136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6410194', 'n_tokens_mistral': 3431, 'n_tokens_neox': 3045, 'n_words': 985}