7

我有一个用 PHP 编写的独立类,用于一些非常基本的 LDAP/AD 功能。我想在我正在使用 cakephp 的项目中使用这个类。

看起来在 cakephp 1.2 中我可以将类添加为供应商,但看起来 cakephp 1.3 删除了对供应商的支持。那么我将如何从这个类中调用一些函数呢?

(我想尽量保持类本身不变,而不是把它变成一个插件,因为这似乎没有必要)

谢谢!

下面的代码:

**<?php
class UsersController extends AppController {

var $name = 'Users';

    //commented out because it breaks the script
    //App::import('Lib', 'ldap');


function index() {
    $this->User->recursive = 0;
    $this->set('users', $this->paginate());
}

    function login() {

        if (!empty($this->data)) {
                if ($ldap->auth($this->data['User']['user'],$this->data['User']['password'])) {
                        $this->Session->setFlash(__('The user has been saved', true));
                        $this->Session->write('user', $this->data['User']['user']);
                        $this->redirect(array('action' => 'index'));
                } else {
                        $this->Session->setFlash(__('Login Failed', true));
                }
        }
    }

    function logout() {
        $this->Session->delete('user');
        $this->redirect($this->referer());

    }

function view($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid user', true));
        $this->redirect(array('action' => 'index'));
    }
    $this->set('user', $this->User->read(null, $id));
}

function add() {
    if (!empty($this->data)) {
        $this->User->create();
        if ($this->User->save($this->data)) {
            $this->Session->setFlash(__('The user has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
        }
    }
    $projects = $this->User->Project->find('list');
    $this->set(compact('projects'));
}

function edit($id = null) {
    if (!$id && empty($this->data)) {
        $this->Session->setFlash(__('Invalid user', true));
        $this->redirect(array('action' => 'index'));
    }
    if (!empty($this->data)) {
        if ($this->User->save($this->data)) {
            $this->Session->setFlash(__('The user has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
        }
    }
    if (empty($this->data)) {
        $this->data = $this->User->read(null, $id);
    }
    $projects = $this->User->Project->find('list');
    $this->set(compact('projects'));
}

function delete($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid id for user', true));
        $this->redirect(array('action'=>'index'));
    }
    if ($this->User->delete($id)) {
        $this->Session->setFlash(__('User deleted', true));
        $this->redirect(array('action'=>'index'));
    }
    $this->Session->setFlash(__('User was not deleted', true));
    $this->redirect(array('action' => 'index'));
}
}
?>**
4

2 回答 2

8

Cake 1.3 仍然完美地支持供应商文件的想法。此外,它们现在还支持“库”,即不是 3rd 方类的附加类。只需将文件弹出到/vendorsor/libs目录并使用App::import.

于 2010-07-22T23:45:29.803 回答
3

我让它工作了,我不得不调用“App::import('Lib', 'ldap');” 在控制器类之外,然后在我想要的函数中将其作为新类调用。

下面是最终结果

<?php
App::import('Lib', 'ldap');
class UsersController extends AppController {

    var $name = 'Users';

    function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

        function login() {

            if (!empty($this->data)) {
                $ldap = new ldap;
                    if ($ldap->auth($this->data['User']['user'],$this->data['User']['password'])) {

                            if (!$this->User->findByUser($this->data['User']['user']) )
                            {
                                $ldap_info = $ldap->getInfo($this->data['User']['user']);
                                $this->data['User']['name'] = $ldap_info['name'];
                                $this->add();
                            }

                            $this->Session->write('user', $this->data['User']['user']);
                            $this->redirect(array('action' => 'index'));
                    } else {
                            $this->Session->setFlash(__('Login Failed', true));
                    }
            }
        }

        function logout() {
            $this->Session->delete('user');
            $this->redirect($this->referer());

        }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    private function add() {
        if (!empty($this->data)) {
            $this->User->create();
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        $projects = $this->User->Project->find('list');
        $this->set(compact('projects'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $id);
        }
        $projects = $this->User->Project->find('list');
        $this->set(compact('projects'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for user', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->User->delete($id)) {
            $this->Session->setFlash(__('User deleted', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Session->setFlash(__('User was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }
}
?>
于 2010-07-27T21:37:52.830 回答