<?php
/**
 * A model for the whole list of users.
 *
 * @package todojsc
 */

class Todojsc_Model_Users extends Todojsc_Model_Acl_Abstract
{
    public function getResourceId()
    {
        return 'User';
    }

    public function setAcl(Todojsc_Acl_Interface $acl)
    {
        if (!$acl->has($this->getResourceId('User')))
        {
            $acl->add($this)
                ->allow('Guest', $this, array('login'))
                ->allow('User', $this, array('login', 'users'))
                ->allow('Secretary', $this, array('login', 'users', 'edit-user'));
        }
        $this->_acl = $acl;
    }
    
    public function getAcl()
    {
        if (null === $this->_acl)
        {
            $this->setAcl(new Todojsc_Model_Acl_Todojsc());
        }
        return $this->_acl;
    }
    
    public function getUserById($id)
    {
        return $this->getResource('User')->getUserById($id);
    }

    public function getUserByName($name)
    {
        return $this->getResource('User')->getUserByName($name);
    }

    public function getUsers()
    {
        if (!$this->checkAcl('users'))
        {
            throw new SF_Exception('Not enough rights to display the users list.');
        }
        return $this->getResource('User')->getUsers();
    }

    public function editUser($post)
    {
        if (!$this->checkAcl('edit-user'))
        {
            throw new SF_Exception('Not enough rights to edit an user.');
        }
        $form = $this->getForm('edit-user');
        if (!$form->isValid($post))
        {
            return false;
        }
        $data = $form->getValues();
        if (array_key_exists('username', $data))
        {
            $user = $this->getResource('User')->getUserByName($data['username']);
            if (null == $user)
            {
                //TODO: First need to managed the whole coordinate tables through the user item.
                //$this->getResource('User')->saveRow($data);
            }
            else
            {
                return false;
            }
        }
        return false;
    }
    
    public function registerUser($post)
    {
        if (!$this->checkAcl('register'))
        {
            throw new SF_Exception('Not enough rights to register a new user.');
        }
        $form = $this->getForm('register');
        if (!$form->isValid($post))
        {
            return false;
        }
        $data = $form->getValues();

        if (array_key_exists('password', $data))
        {
            if ('' != $data['password'])
            {
                $data['salt'] = md5($this->createSalt());
                $data['password'] = sha1($data['password'].$data['salt']);
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }

        if (array_key_exists('username', $data))
        {
            $data['role'] = 'Admin';
            $user = $this->getResource('User')->getUserByName($data['username']);
            if (null == $user)
            {
                $this->getResource('User')->saveRow($data);
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
        return true;
    }

    private function createSalt()
    {
        $salt = '';
        for($i = 0; $i <  50; $i++)
        {
            $salt .= chr(rand(33,126));
        }
        return $salt;
    }
}

