CAS Authentication with Yii Framework - php

I'm facing some troubles trying to implement CAS Authentication with Yii framework. I downloaded the client classes and everything is working when I'm not using any framework, but when I try to integrate with Yii validation seems to not be working at all.
Can Anyone please describe the process to successfuly make a CAS validation with yii.
I put the clients files under /myapp/protected/vendor directory, then I put the following lines at the begining of the controller
Yii::import('application.vendors.*');
require_once('CAS/CAS.php');
Then I created the method for use my CAS Classes
public function casValidation()
{
phpCAS::client(CAS_VERSION_2_0, $server, $port, "/cas",false);
phpCAS::setNoCasServerValidation();
phpCAS::forceAuthentication();
if(isset($_REQUEST['logout']))
{
phpCAS::logout();
}
}
And Finally I try to validate from my index action
public function actionIndex()
{
$this->casValidation();
$this->render('index');
}
But in the web browser shows the following error
PHP warning
include(CAS_Client.php) [function.include]: failed to open >stream: No such file or directory
C:\app\vertrigo\Vertrigophp52\www\testyii\framework\YiiBase.php(427)
Am I Missing something? What should I do to make things work?

Finally I got it working, turns out that I had made a couple of mistakes.
First of all I should not have been using require statement. The reason for this is that Yii alredy take care of loading the classes via the import method as shown below.
Yii::import('application.vendor.CAS.*')
Second thing I had a typo on this import, I had
/* This is wrong */
Yii::import('application.vendors.CAS.*')
instead of
/* This is correct */
Yii::import('application.vendor.CAS.*')
So Yii was looking for the vendors directory which does not exist.
And finally, to successfully use the import method the file containing your class has to have the same name. So if you have a class name user you have to name your file user.php in order to Yii can load it successfully.

Related

Template file "Error/error500.ctp" is missing. during Unit testing in CakePHP

I have set up PHPUnit into my CakePHP application,
but when I try to run it I receive this error:
Cake\View\Exception\MissingTemplateException: Template file "Error/error500.ctp" is missing.
I have tried several times with different settings of the phpunit.xml.dist page and nothing changes.
Here is the code:
public function testLogin()
{
$this->get("/admin/users/login");
$this->assertResponseOk();
}
Very simple method I am try to test within the tests/TestCase/Controller/Admin/UsersControllerTest.php
make sure error500.ctp is present in template/Errro/ directory

CodeIgniter - Loading an application package prevents loading of view and dealing with collisions

I've been learning CodeIgniter and was just experimenting with adding Application Packages.
In the default install I've added a package path to the third_party folder that contains a single view, and then I want it to continue loading the default welcome_message. Separately this all works fine, but together the welcome_message view file can't be found apparently. Reading on in the docs at http://ellislab.com/codeigniter/user-guide/libraries/loader.html it mentions view collisions, and talks about setting the second parameter. Okay no problem, there isn't another view named welcome_message, but I do what then mention according to the example provided, which sets it to FALSE to get the welcome_message to display, but that doesn't work.
In fact I have to set it to TRUE to get it to work, which is the exact opposite of the docs. Can someone explain this logic reversal? and regarding view naming collisions due to a lack of a description in the docs does this mean setting the second param to whichever boolean prevents collisions and allow full use of all views regardless of naming? Or does it simply throw an error instead of loading the improper view?
class Welcome extends CI_Controller {
public function index()
{
$this->load->add_package_path(APPPATH . 'third_party/foo_bar/', FALSE);
$this->load->view('foo_bar');
$this->load->view('welcome_message'); // throws err on FALSE and loads on TRUE in add_package_path() call
}
}
After $this->load->view('foo_bar');
reset the path using $this->load->remove_package_path();
When you use add_package_path CI will check that folder for all path requests. This is fine when you are working with a self contained app. When you are done with that and want to access the "regular" CI paths for views etc, you need to remove the package path first.

CodeIgniter - Why does library load fine in view but not in controller

I'm using Stripe's php library. DL link: https://code.stripe.com/stripe-php-latest.zip
I've included the library like so inside of a view:
require_once(APPPATH.'libraries/stripe/lib/stripe.php');
Everything works fine when I do this in a view, but if I attempt to do it in a controller I get a server error. Why?
I have tried codeigniters load library method, but still a server error. I've change all capitals to lowercases, still the error.
Loading Packages:
$this->load->add_package_path(APPPATH.'third_party/stripe/');
$this->load->library('stripe');
Documentation: Application "Packages" section
http://ellislab.com/codeigniter/user-guide/libraries/loader.html
If you use third_party libraries, like for example the Facebook PHP SDK, use the following code:
$this->_obj->load->add_package_path(APPPATH.'third_party/facebook/');
$this->_obj->load->library('facebook', $config);
$this->_obj->load->remove_package_path(APPPATH.'third_party/facebook/');
And place the files for this library in the /third_party/facebook/libraries/ folder.
Note: It's a common mistake to place the files directly in '/facebook/' instead of in '/libraries/' (and also not mentioned in the documentation, too bad).
You can use the stripe native library by adding as codeigniter helper.
step 1: put the stripe package into helper directory
step 2: rename the init.php to init_helper.php
step 3: load the helper into your controller
$this->load->helper('stripe/init');
step 4: call stripe library
try
{
// set api key
\Stripe\Stripe::setApiKey('YOUR_SECRET_STRIPE_API_KEY');
// create customer
$customer_detail = \Stripe\customer::create(array(
'email' => 'customer_email#testdomain.com'
));
echo $customer_detail->id;
}
catch (Exception $e)
{
$error = $e->getMessage();
echo $error;
}
Try putting your library on application/libraries and then load your librarie with
$this->load->libraries('Stripe.php');

Writing a cron job using CodeIgniter CLI?

Hi I am facing some problems in writing a cron job using CI CLI way. My application has a controller name called manager.php in that there is method called check_status where I am gonna get all the order_ids using one model function. Ever order_id row had a status filed in database which either success or failure.
I have an api if i pass order_id to that it will tell whether order is successfully delivered or not. But here comes the problem I have below line in controller in the top.
<?php if(! defined('BASEPATH') ) exit("NO Direct Script Access Allowed"); ?>
So when i try to run method check_status from CLI in CI it gives me an error stating NO Direct Script Access Allowed.
This is the way i called above method php application/controllers/manager.php check_status
So i decided like this i created an another class file called cron_job.php in that i didn't keep the above error line "No Direct Script Access Allowed". I thought it will give access now when i try to run but it doesn't give an error and even output also.
This is the class which i created and method in that.
<?php
class Cron_job extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
?>
I run this controller form CLI like this php application/controller/cron_job.php message
Note: I am in ROOT directory.
No Output at all. So i tried in another way like this php index.php application/controller/cron_job.php message
Now it gives me error stating that Error 404 page not found.
What i tried in another way now i created a file in views folder and in that i am calling old controller/method url like below.
$result = file_get_contents("http://application_path/controller/method");
echo $result;
Now i am getting output which i defined in the method check_status in manager.php controller.
But here comes another problem now after the above line i will get an array output which had all the order_ids.
I am gonna send this each id to a api to check status. If it is failure it will check whether it is delivered or not. If it's done i need to update that status in the database against that order_id. But now i am in view file, is it possible to call a model file from the view file or is there any way to do this.
Any help?
Note: There is no syntax errors in any controller or any method , which are fully verified and working normally when i am accessing using urls.
You need to read the CodeIgniter help section on Running via the Command Line. It's very easy. Your original approach was correct. But you do not call your controller method directly by its path, instead CD to your project root and then the call the index.php file with the controller and method as parameters.
// This is how you call CI via the command line.
// Use spaces between index.php and your arguments.
$ php index.php <controller> <method> [params]
// And in your instance
$ php index.php manager check_status [param1 param2 param3]
Depending on your host you may need to call the PHP version compiled for CLI.
the ci helper will help you in this.
1 ) Create a helper & create a function in it, that calls your model function
function getUserDetails($userId = '') {
$CI = & get_instance();
$getUserDetailsByUserId = $CI->user_model->getUserDetailsByUserId($userId);
return $getUserDetailsByUserId;
}
2) Now you can call getUserDetails($userId); in your view.

CodeIgniter: Can't Get My New Controller/View To Show

I am learning how to use codeIgniter as my php framework. I am reading through the documentation and watching the intro video and just generally following along with the first tutorial, but it's not working for me.
I have created a controller called "test.php" and a view called "test_view". The controller for this class is exactly like "welcome.php" and the view file just has some static html. However, when I go to index.php/test I get a 404 error.
I have also tried manipulating the original welcome files so that instead of calling a view it just echos "testing", yet I still see the original welcome message! I've tried clearing my browsing cash and refreshing, but to no avail.
Any suggestions? Thanks.
Edit: Here's the code for controllers/test.php
<?php
class Test extends Controller {
//Just trying to get it to echo test
public function index()
{
echo "test";
//$this->load->view('test_view');
}
}
?>
Try looking at this page in the documentation - this might solve your problem.
This basically means you should try typing index.php?/test/ instead (notice the question-mark).
First of all, check the above link. Might be useful.
If not, then...
Try changing the default controller in the config file ('routes.php') to something else (probably, to 'test'), then try loading index.php. Just to test whether the whole system works (or not).
Check whether mod_rewrite is loaded (in your server .conf-file, if you're using Apache).
Try using the latest build of the framework. AFAIK, the name of the controller class for now is "CI_Controller".
Finally, try removing the word 'public' before the declaration of the function. AFAIR, CI enable you to make private functions in controllers just by using prefix (which is set in the config file) at the beginning of the name of the function (thus making all the other functions public).
But most certainly the problem is with the mod_rewrite. If not, try debugging with die('Page found'); instead of echo - this will allow you to track possible redirects on the page.

Categories