Added methods to set data in projects

Added 'add' program to add a user to a project
Esse commit está contido em:
Felipe N. Moura
2012-06-30 03:17:55 -03:00
commit 08b28bd237
12 arquivos alterados com 263 adições e 35 exclusões
+3 -2
Ver Arquivo
@@ -27,6 +27,7 @@ class En implements l10n{
else
return false;
}
public function __construct()
{
$this->messages['welcome'] = "Welcome to mind3rd.\nPlease, type 'help' to see the basic help content.\nType 'list' to see the complete list of commands.\n";
@@ -60,7 +61,7 @@ class En implements l10n{
$this->messages['theosDBQrOk'] = Mind::message("Theos: Database created successfuly", '[Ok]', false);
$this->messages['dbDriverNotFound'] = Mind::message("Theos: Database Driver not found", '[Fail]', false);
$this->messages['missingParameter'] = Mind::message("API: Missing argument: %s", '[Fail]', false);
$this->messages['done'] = Mind::message("API: Done\n", '[Ok]', false);
$this->messages['done'] = Mind::message("API: Done", '[Ok]', false);
$this->messages['invalidOptionValue'] = Mind::message("API: '%s' is not a valid option for '%s'", '[Fail]', false);
$this->messages['runnintPHPUnit'] = "Please wait...running PHPUnit...\n";
$this->messages['mustBeAdmin'] = Mind::message("API: You must be the admin to perform this action", '[Fail]', false);
@@ -68,7 +69,7 @@ class En implements l10n{
$this->messages['wrongParam'] = Mind::message("\nMain: The parameter '%s', whose value is '%s', is not valid!", '[Fail]', false);
$this->messages['phpunitNotFound'] = "You must specify where to find phpUnit classes\n".
"You can configure it on mind3rd/env/mind.ini ini file\n".
"changing the phpunit-src ini property\n";;
"changing the phpunit-src ini property\n";
$this->messages['http_invalid_requisition'] = <<<MESSAGE
Invalid HTTP requisition.
+22 -1
Ver Arquivo
@@ -154,6 +154,27 @@ class Project{
return $this->db->execute($qr);
}
public function addUser($user){
if(!\Mind::hasProject($this->data['name'], $user['pk_user'])){
$qr_userProj= "INSERT into project_user
(
fk_project,
fk_user
)
values
(
".$this->data['pk_project'].",
".$user['pk_user']."
)";
return $this->db->execute($qr_userProj);
}else{
return true;
//echo "JA TINHA\n";
}
//var_dump($this->data['pk_project']);
}
/**
* Marks an entity as dropped from the last version.
* The next version has not the passed entity, so, it will be marked
@@ -162,7 +183,7 @@ class Project{
* @param array $en
* @return boolean True in case of success, otherwise, generates an error
*/
public function markAsDopped(Array $en)
public function markAsDropped(Array $en)
{
$qr= "UPDATE entity
SET status= ".\COMMIT_STATUS_DROP."
+2 -2
Ver Arquivo
@@ -68,7 +68,7 @@ class ProjectFactory extends Project{
}
/**
* Saves the current entities
* Saves the current entities.
*
* It will persist the current entities into the SQLite database.
* This will also verifies the changes in the previous version and
@@ -107,7 +107,7 @@ class ProjectFactory extends Project{
foreach($currentEntities as $en)
{
$commited= true;
$this->markAsDopped($en);
$this->markAsDropped($en);
}
if($commited)
+5 -4
Ver Arquivo
@@ -182,9 +182,9 @@
* @param String $project
* @return boolean
*/
static function hasProject($project)
static function hasProject($project, $u=false)
{
return MindProject::hasProject($project);
return MindProject::hasProject($project, $u);
}
/**
@@ -192,11 +192,12 @@
* Alias for MindProject::openProject.
*
* @param AssocArray $p
* @param Boolean $silent Do not output anything
* @return boolean
*/
public static function openProject($p)
public static function openProject($p, $silent=false)
{
return MindProject::openProject($p);
return MindProject::openProject($p, $silent);
}
/**
+166 -8
Ver Arquivo
@@ -12,8 +12,18 @@
*/
class MindProject extends VersionManager{
public static $sourceContent= Array();
public static $currentSource= null;
public static $sourceContent = Array();
public static $currentSource = null;
public static $adminValidAttrs = Array('creator', 'info', 'name');
public static $availableAttrs = Array('idiom',
'technology',
'title',
'database_drive',
'database_addr',
'database_name',
'database_port',
'database_user',
'database_pwd');
/**
* Uses the QueryFactory to return the sql to create all the database.
@@ -32,6 +42,126 @@ class MindProject extends VersionManager{
return $qrs;
}
/**
* Retrieves the Project Object, found by its name.
*
* Must be an administrator to call this method.
*
* @param String $pName
* @return ProjectObject Or false, if does not exist
*/
public static function getProjectByName($pName){
if(!\MindUser::isAdmin()){
\Mind::write('mustBeAdmin');
return false;
}
$db= self::getDBConn();
$project= false;
$projs= $db->query("SELECT * from project where name = '".addslashes($pName)."'");
foreach($projs as $k=>$p)
{
$project= $p;
break;
}
return $project;
}
protected static function getDBConn()
{
if(!self::$dbConn)
self::$dbConn= new \MindDB();
return self::$dbConn;
}
/**
* Sets a property of the currend project.
*
* If admin, you can pass the project to be changed.
* This method actually changes AND PERSISTS the change to the
* database.
*
* @param String $attr
* @param Mixed $value
* @param String $proj
* @return boolean
*/
public static function set($attr, $value, $proj=false)
{
if(\in_array($attr, self::$adminValidAttrs) || $proj){
if(!\MindUser::isAdmin()){
\Mind::write('mustBeAdmin');
return false;
}
}
if(\in_array($attr, self::$adminValidAttrs)){
if($proj && !is_numeric($proj)){
$proj= \MindProject::getProjectByName($proj);
if(!$proj){
\MindSpeaker::write('noProject', true, $proj);
return false;
}
$proj= $proj['pk_project'];
}
$pk_project= $proj?
\MindProject::getProjectByName($proj):
isset(Mind::$currentProject['pk_project'])?
Mind::$currentProject['pk_project']:
false;
if(!$pk_project){
\MindSpeaker::write('noProject', true, $proj);
return false;
}
$qr_updProj= "UPDATE project
SET ".$attr."=".(is_numeric($val)? $val: "'".$val."'")."
WHERE pk_project = ".$pk_project;
$db= self::getDBConn();
if($db->execute($qr_updProj))
return true;
else
return false;
}else{
$proj= $proj?
$proj:
isset(Mind::$currentProject['pk_project'])?
Mind::$currentProject['name']:
false;
if(!$proj){
\MindSpeaker::write('noProject', true, $proj);
return false;
}
$iniSource= Mind::$projectsDir.$proj.'/mind.ini';
if(!file_exists($iniSource)){
\MindSpeaker::write('noProject', true, $proj);
return false;
}
try{
$iniContent= file_get_contents($iniSource);
}catch(Exception $e){
\MindSpeaker::write('permissionDenied', true, $proj);
return false;
}
$attr= trim($attr);
if(!\in_array($attr, self::$availableAttrs)){
\MindSpeaker::write('invalidCreateParams');
return false;
}
$iniContent= preg_replace("/".$attr."(( |\t)+)?=.+(\n|$)/", $attr."=".$value."\n", $iniContent);
try{
file_put_contents($iniSource, $iniContent);
\MindProject::reload();
return true;
}catch(Excepption $e){
\MindSpeaker::write('permissionDenied', true, $proj);
return false;
}
}
}
/**
* Uses the QueryFactory to return the sql to create all the database.
*
@@ -89,7 +219,7 @@ class MindProject extends VersionManager{
* @param String $project
* @return boolean
*/
static function hasProject($project)
static function hasProject($project, $u=false)
{
GLOBAL $_MIND;
$projectfile= Mind::$projectsDir.$project;
@@ -100,10 +230,15 @@ class MindProject extends VersionManager{
project.name as name
from project_user,
project
where fk_user= ".$_SESSION['pk_user']."
and project.name = '".$project."'
where project.name = '".$project."'
and fk_project = pk_project
";
if(!\MindUser::isAdmin())
$hasProject.= " and fk_user= ".$_SESSION['pk_user'];
else if($u)
$hasProject.= " and fk_user= ".((int)$u);
$data= $db->query($hasProject);
if(sizeof($data)>0)
foreach($data as $row)
@@ -114,6 +249,8 @@ class MindProject extends VersionManager{
if(!file_exists($projectfile) || $noAccess)
{
if($u && \MindUser::isAdmin())
return false;
Mind::write('noProject', true, $project);
return false;
}
@@ -173,19 +310,40 @@ class MindProject extends VersionManager{
set_include_path(get_include_path() . PATH_SEPARATOR . $langPath);
}
public static function renew(){
if(isset($_SESSION['currentProjectName']) && $_SESSION['currentProjectName']){
if(!$projectData= \Mind::hasProject($_SESSION['currentProjectName']))
return false;
\MindProject::close();
\Mind::openProject($projectData, true);
}else{
\MindSpeaker::write('currentProjectRequired');
}
}
public static function reload(){
self::renew();
}
public static function close(){
$_SESSION['currentProject']= false;
Mind::$project= false;
Mind::$currentProject= false;
//session_unset($_SESSION['currentProject']);
}
/**
* Loads data from the passed project
*
* @param AssocArray $p
* @return boolean
*/
public static function openProject($p)
public static function openProject($p, $silent=false)
{
GLOBAL $_REQ;
if(Mind::$project)
{
if($_SESSION['currentProject'] != $p['pk_project'])
Mind::$project->close();
self::close();
else
return Mind::$project;
}
@@ -217,7 +375,7 @@ class MindProject extends VersionManager{
Mind::$currentProject['pk_version']= $pF->data['pk_version'];
Mind::$project= $pF;
if($msg)
if($msg && !$silent)
{
Mind::write('projectOpened', true, $p['name']);
}
+12
Ver Arquivo
@@ -21,6 +21,18 @@ class MindUser
return sha1($pwd);
}
/**
* Sets a property of the currend user.
*
* If admin, you can pass the user to be changed.
* This method actually changes AND PERSISTS the change to the
* user's base.
*
* @param String $attr
* @param Mixed $value
* @param Integer $user
* @return boolean
*/
public static function set($attr, $value, $user=false)
{
if(\in_array($attr, self::$adminValidAttrs) || $user)
+5
Ver Arquivo
@@ -94,6 +94,11 @@
return MindProject::analyze(false);
}
public static function set($attr, $val, $proj)
{
return \MindProject::set($attr, $val, $proj);
}
/**
* Gets an array with all the source code, for the current project.
* @return Array
+13 -1
Ver Arquivo
@@ -58,9 +58,21 @@ class User{
return \MindProject::projectsList();
}
/**
* Sets a property of the currend user.
*
* If admin, you can pass the user to be changed.
* This method actually changes AND PERSISTS the change to the
* user's base.
*
* @param String $attr
* @param Mixed $value
* @param Integer $user
* @return boolean
*/
public static function set($attr, $value, $user=false)
{
\MindUser::set($attr, $value, $user);
return \MindUser::set($attr, $value, $user);
}
public static function isAdmin()
+16 -4
Ver Arquivo
@@ -10,7 +10,7 @@
Symfony\Component\Console;
/**
* This class represents a model for programs
* This class represents a model for programs.
*
* @author Felipe Nascimento de Moura <felipenmoura@gmail.com>
*/
@@ -35,10 +35,22 @@
echo "project does not exist";
return false; // TODO: put it into L10N
}
$pF= new DAO\ProjectFactory(Array('name'=>$this->project));
if(!$projectData= \Mind::hasProject($this->project))
return false;
$pF= new DAO\ProjectFactory($projectData);
if($pF->addUser(\MindUser::getUserByLogin($this->user))){
\MindSpeaker::write('done');
return true;
}else{
return false;
}
//var_dump($pF);
//print_r($pF);
echo $this->project."\n\n";
echo "BBBBBBBBB";
//echo $this->project."\n\n";
//echo "BBBBBBBBB";
//$pF= new DAO\ProjectFactory(Mind::$currentProject);
//$user= \API\User::loadUserInfo($this->user);
+1 -1
Ver Arquivo
@@ -51,7 +51,7 @@
}
Mind::write('autenticated', true, $this->login);
\MindLog::log(\MindLog::LOG_TYPE_USER, ' OK - \''.$this->login.'\' logged in');
\MindProject::close();
return $this;
}
+8 -12
Ver Arquivo
@@ -21,7 +21,7 @@
public $value = '';
public $extra = '';
private function setUserData()
/*private function setUserData()
{
if($this->attribute=='pwd')
$this->value= $this->prompt('pwd', 'The new password, please', true);
@@ -36,12 +36,11 @@
{
echo "Project's data\n";
}
*/
public function executableFunction()
{
$property= explode('.', $this->property);
if(sizeof($property) <= 1){
//\MindSpeaker::write('wrongParam', $this->property);
\MindSpeaker::write('wrongParam', true, "property", $this->property);
return false;
}
@@ -55,19 +54,16 @@
$this->value= $this->answers['pwd'];
}
if($this->extra){
\MindUser::set($property, $this->value, $this->extra);
if(\MindUser::set($property, $this->value, $this->extra))
\MindSpeaker::write('done');
}else{
\MindUser::set($property, $this->value);
if(\MindUser::set($property, $this->value))
\MindSpeaker::write('done');
}
}else{
if(\MindProject::set($property, $this->value, $this->extra))
\MindSpeaker::write('done');
}
/*if($this->whose == 'user')
{
$this->setUserData();
}else{
$this->setProjectData();
}*/
}
public function __construct()
+10
Ver Arquivo
@@ -37,6 +37,7 @@ EOT
'info',
'data',
'props',
'project',
'properties',
'lobes');
asort($opts);
@@ -71,6 +72,14 @@ EOT
$this->printMatrix($projectList);
}
break;
case 'project':
$p= \Mind::$currentProject;
if(!$p)
\MindSpeaker::write("currentProjectRequired");
else{
echo " ".\Mind::$currentProject['name'].": ".\Mind::$currentProject['title']."\n";
}
break;
case 'data':
case 'props':
case 'properties':
@@ -91,6 +100,7 @@ EOT
}else{
\MindSpeaker::write("currentProjectRequired");
}
// TODO: list users IN THIS project
break;
case 'source':
$p= \Mind::$currentProject;