Skip to content

Commit

Permalink
Adding user plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwin Date committed Oct 6, 2012
1 parent afc868e commit 6440786
Show file tree
Hide file tree
Showing 5 changed files with 443 additions and 0 deletions.
23 changes: 23 additions & 0 deletions users/users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @package API
* @version 1.5
* @author Brian Edgerton
* @link http://www.edgewebworks.com
* @copyright Copyright (C) 2011 Edge Web Works, LLC. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*/

defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.plugin.plugin');

class plgAPIUsers extends ApiPlugin
{
public function __construct()
{
parent::__construct();

ApiResource::addIncludePath(dirname(__FILE__).'/users');
}
}
18 changes: 18 additions & 0 deletions users/users.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<install version="1.7" type="plugin" group="api" method="upgrade">
<name>Api - users</name>
<version>1.0</version>
<creationDate>April 2012</creationDate>
<author>Tekdi</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.tekdi.com</authorUrl>
<copyright>2012 users</copyright>
<license>GNU General Public License v2</license>
<description>This plugin exposes users to the Joomla! API</description>

<files>
<filename plugin="users">users.php</filename>
<filename >users/users.php</filename>
<folder>users</folder>
</files>
</install>
186 changes: 186 additions & 0 deletions users/users/users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php
/**
* @package API
* @version 1.5
* @author Brian Edgerton
* @link http://www.edgewebworks.com
* @copyright Copyright (C) 2011 Edge Web Works, LLC. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*/

defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.plugin.plugin');
jimport('joomla.html.html');
jimport('joomla.user.helper');
jimport( 'joomla.application.component.helper' );
jimport( 'joomla.application.component.model' );
jimport( 'joomla.database.table.user' );
require_once( JPATH_SITE .DS.'components'.DS.'com_community'.DS.'libraries'.DS.'core.php');
require_once( JPATH_SITE .DS.'libraries'.DS.'joomla'.DS.'filesystem'.DS.'folder.php');
//vishal
if(JFolder::exists(JPATH_BASE.DS.'components'.DS.'com_xipt'))
{
require_once( JPATH_SITE .DS.'components'.DS.'com_xipt'.DS.'api.xipt.php');
}

class UsersApiResourceUsers extends ApiResource
{

public function post()
{

$error_messages = array();
$fieldname = array();
$response = NULL;
$validated = true;
$userid = NULL;
$data = JRequest::get('post');
//for rest api only
unset($data['format']);
unset($data['resource']);
unset($data['app']);
unset($data['password']);
unset($data['username']);
//
//$userid = $data['userid'];
$fields = $data['field'];

//chk data
if($data['email']=="" )
{
$validated = false;
$error_messages[] = array("id"=>1,"fieldname"=>"email","message"=>"Email cannot be blank");
} elseif( false == $this->isValidEmail( $data['email'] ) ) {
$validated = false;
$error_messages[] = array("id"=>1,"fieldname"=>"email","message"=>"Please set valid email id eg.([email protected]). Check 'email' field in request");

}else
{
//check mail is registerd
$isemail =& CFactory::getModel('register');
$found = $isemail->isEmailExists(array('email'=>$data['email'] ));
if($found)
{
$validated = false;
$error_messages[] = array("id"=>1,"fieldname"=>"email","message"=>"email already exist and user is registered");
}
}

if( $data['password1']=="" )
{
$validated = false;
$error_messages[] = array("id"=>1,"fieldname"=>"password","message"=>"Password cannot be blank");
}

if( $data['name']=="" or $data['username1']=="" )
{
$validated = false;
$error_messages[] = array("id"=>1,"fieldname"=>"name","message"=>"Name cannot be blank");
}

if( true == $validated)
{ //to create new user for joomla

global $message;
jimport('joomla.user.helper');
$authorize = & JFactory::getACL();
$user = clone(JFactory::getUser());
$user->set('username', $data['username1']);
$user->set('password', $data['password1'] );
$user->set('name', $data['name']);
$user->set('email', $data['email']);

// password encryption
$salt = JUserHelper::genRandomPassword(32);
$crypt = JUserHelper::getCryptedPassword($user->password, $salt);
$user->password = "$crypt:$salt";

// user group/type
$user->set('id', '');
$user->set('usertype', 'Registered');
if(JVERSION >= '1.6.0')
{
$userConfig = JComponentHelper::getParams('com_users');
// Default to Registered.
$defaultUserGroup = $userConfig->get('new_usertype', 2);
$user->set('groups', array($defaultUserGroup));
}
else
$user->set('gid', $authorize->get_group_id( '', 'Registered', 'ARO' ));

$date =& JFactory::getDate();
$user->set('registerDate', $date->toMySQL());

// true on success, false otherwise
if(!$user->save())
{
$message="not created because of ".$user->getError();
return false;
}
else
{
$message="created of username-".$user->username." and send mail of details please check";

}
//$this->plugin->setResponse($user->id);
$userid = $user->id;
//create profile
$profileModel =& CFactory::getModel('profile');
$val = $profileModel->saveProfile($userid, $fields);
//result message
$result = array('user id '=>$userid,'message'=>$message);
$result =($userid) ? $result : $message;

$this->plugin->setResponse($result);


}
else
{

$this->plugin->setResponse($error_messages);//print_r($error_messages); die("validate mail2222");
}



}

public function put()
{
$data = JRequest::get('post');

//for rest api only
unset($data['format']);
unset($data['resource']);
unset($data['app']);
unset($data['password']);
unset($data['username']);
//
$userid = $data['userid'];
$fields = $data['field'];

print_r($data);die;


$this->plugin->setResponse( "in the put method" );
}

public function delete()
{
$this->plugin->setResponse( "in the delete method" );
}

function isValidEmail( $email )
{
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

if ( eregi( $pattern, $email ) )
{
return true;
} else {
return false;
}
}

}
73 changes: 73 additions & 0 deletions users/users/users_delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* @package API
* @version 1.5
* @author Brian Edgerton
* @link http://www.edgewebworks.com
* @copyright Copyright (C) 2011 Edge Web Works, LLC. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*/

// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );
jimport('joomla.user.helper');
jimport( 'joomla.application.component.helper' );
require_once( JPATH_SITE .DS.'components'.DS.'com_community'.DS.'libraries'.DS.'core.php');
require_once( JPATH_SITE .DS.'libraries'.DS.'joomla'.DS.'filesystem'.DS.'folder.php');
require_once( JPATH_ADMINISTRATOR .DS.'components'.DS.'com_users'.DS.'models'.DS.'user.php');
//vaibhav
if(JFolder::exists(JPATH_BASE.DS.'components'.DS.'com_xipt'))
{
require_once( JPATH_SITE .DS.'components'.DS.'com_xipt'.DS.'api.xipt.php');
}

class UsersApiResourceUsers_delete extends ApiResource
{

public function post()
{
//print_r("in post");die;

$error_messages = array();
$fieldname = array();
$response = NULL;
$validated = true;
$userid = NULL;
$data = JRequest::get('post');
//for rest api only
unset($data['format']);
unset($data['resource']);
unset($data['app']);
unset($data['password']);
unset($data['username']);
//
$userid = $data['userid'];

$db = JFactory::getDBO();

$query = "DELETE u, um, cu, cf
FROM #__users AS u
LEFT JOIN #__user_usergroup_map AS um ON um.user_id = u.id
LEFT JOIN #__community_users AS cu ON cu.userid = u.id
LEFT JOIN #__community_fields_values AS cf ON cf.user_id = u.id
WHERE u.id =".$userid."";

$db->setQuery($query);
//$data1 = $db->loadResult();
if (!$db->query())
{
$this->setError( $db->getErrorMsg() );
return false;
}
else
{
$this->plugin->setResponse( "User of id = ".$userid." deleted" );
}


}


}
Loading

0 comments on commit 6440786

Please sign in to comment.