<commit_before><?php

include_once NWEB_ROOT.'/lib/core_auth.php';

include_once 'models.php';

class auth extends ControllerBase{

	private $dal;

	public function __construct(){

		$this->settings['template'] = NWEB_ROOT.'/config/template.d/generic_template.php';
		$this->pageData = array();

		if ( getConfigOption( 'use_db') ){
			$this->dal = new DataAccessLayer();
			$this->dal->registerModel( 'nwUser' );
			$this->dal->registerModel( 'nwGroup' );

		}


	}

	/**
	 * Logs a user in after validating their credentials, 
	 * or redirects back to the login page.
	 */
	public function login(){

		$sessionmgr = SessionMgr::getInstance();
                $sessionmgr->noRedirect = True;


		if ( $sessionmgr->check_csrf('post') && 

			$this->check_login( request::sanitized_post('username'), request::post('pass') ) ){

                        $user = $this->dal->get('nwUser', 'username', request::sanitized_post('username'));
                        if (is_null($user)){
                            $user->id = -1;
                            $user->username = request::sanitized_post('username');
                            
                        }
                        
			log_user_in( $user );
                        
			$redir_to = $sessionmgr->toPage;
			unset( $sessionmgr->toPage );

			$this->pageData['content'] = pullContent( AUTH_ROOT.'/pages/welcome' );

			$this->pageData['id'] = 'login';
			$this->pageData['title'] = "Naterweb Authentication";
			$this->pageData['static'] = AUTH_ROOT.'/pages';
			$this->pageData['to'] = $redir_to;
                        
			render_php_template( $this->settings['template'], $this->pageData );


		} else {

			$this->pageData['content'] = pullContent( AUTH_ROOT.'/pages/login' );

			$this->pageData['id'] = 'login';
			$this->pageData['fail'] = False;
			$this->pageData['title'] = "Naterweb Authentication";
			$this->pageData['static'] = AUTH_ROOT.'/pages';

			render_php_template( $this->settings['template'], $this->pageData );

		}



	}

	/** 
	 * Renders the group management page where groups can 
	 * be added.
	 */
	public function managegroup(){
            

		if ( auth_user( getConfigOption('site_domain').'/?url=auth/managegroup', 'nwadmin' ) ){
                    
                    $sess = SessionMgr::getInstance();
                    $sess->noRedirect = True;

                    $pageData = array();
                    $pageData['groups'] = $this->dal->getAll( 'nwGroup' );
                    $pageData['content'] = pullContent( AUTH_ROOT.'/pages/managegroup');
                    $pageData['static'] = AUTH_ROOT.'/pages';
                    $pageData['title'] = 'Manage Groups';

                    render_php_template( $this->settings['template'], $pageData );
                } else {
                    $this->unauthorized();
                }


	}

	/**
	 * Renders the add group page where a group can be added.
	 */
	public function addgroup(){

		if ( ! auth_user( getConfigOption('site_domain').'/?url=auth/adduser', 'nwadmin' ) ){
                    $this->unauthorized();
                }

		$sessionmgr = SessionMgr::getInstance();
                $sessionmgr->noRedirect = True;


		if ( $sessionmgr->check_csrf('post') && 
			 ! $this->dal->get('nwGroup', 'name', request::sanitized_post('name') ) ){



			$newgroup = new nwGroup();

			$newgroup->name = request::sanitized_post('name');

			$newgroup->save();


			$this->pageData['content'] = pullContent( AUTH_ROOT.'/pages/groupadded' );

			$this->pageData['id'] = 'login';
			$this->pageData['title'] = "Naterweb Authentication";
			$this->pageData['static'] = AUTH_ROOT.'/pages';

			render_php_template( $this->settings['template'], $this->pageData );


		} else {

			$pageData = array();
			$pageData['content'] = pullContent( AUTH_ROOT.'/pages/addgroup' );
			$pageData['static'] = AUTH_ROOT.'/pages';
			$pageData['title'] = 'Add New User';
			$pageData['id'] = 'adduser';

			render_php_template( $this->settings['template'], $pageData );


		}


	}

	/**
	 * Deletes a user from the database along with 
	 * their group associations.
	 */
	public function deluser(){

		if ( ! auth_user( getConfigOption('site_domain').'/?url=auth/manage', 'nwadmin' ) ){
                    $this->unauthorized();
                }

		$sessionmgr = SessionMgr::getInstance();
                $sessionmgr->noRedirect = True;

		$user = $this->dal->get( 'nwUser', 'id', request::sanitized_get( 'uid') );

		foreach ($user->getRelated( 'groups' ) as $g) {
			$user->removeRelated( 'groups', $g );
		}

		$user->delete();

		$redir = new Redirect( 'auth/deluser', '/?url=auth/manage');
		$redir->apply(302);




	}


	/** 
	 * Updates a user's information and groups
	 */
	public function changeuser(){

		if ( ! auth_user( getConfigOption('site_domain').'/?url=auth/adduser', 'nwadmin' ) ){
                    $this->unauthorized();
                }

		$sessionmgr = SessionMgr::getInstance();
                $sessionmgr->noRedirect = True;


		if ( $sessionmgr->check_csrf('post') && 
			  $this->dal->get('nwUser', 'id', request::sanitized_post('uid') ) ){

<commit_msg>fix indentation issue<commit_after><?php

include_once NWEB_ROOT.'/lib/core_auth.php';

include_once 'models.php';

class auth extends ControllerBase{

	private $dal;

	public function __construct(){

		$this->settings['template'] = NWEB_ROOT.'/config/template.d/generic_template.php';
		$this->pageData = array();

		if ( getConfigOption( 'use_db') ){
			$this->dal = new DataAccessLayer();
			$this->dal->registerModel( 'nwUser' );
			$this->dal->registerModel( 'nwGroup' );

		}


	}

	/**
	 * Logs a user in after validating their credentials, 
	 * or redirects back to the login page.
	 */
	public function login(){

		$sessionmgr = SessionMgr::getInstance();
                $sessionmgr->noRedirect = True;


		if ( $sessionmgr->check_csrf('post') && 

			$this->check_login( request::sanitized_post('username'), request::post('pass') ) ){

                        $user = $this->dal->get('nwUser', 'username', request::sanitized_post('username'));
                        if (is_null($user)){
                            $user->id = -1;
                            $user->username = request::sanitized_post('username');
                            
                        }
                        
			log_user_in( $user );
                        
			$redir_to = $sessionmgr->toPage;
			unset( $sessionmgr->toPage );

			$this->pageData['content'] = pullContent( AUTH_ROOT.'/pages/welcome' );

			$this->pageData['id'] = 'login';
			$this->pageData['title'] = "Naterweb Authentication";
			$this->pageData['static'] = AUTH_ROOT.'/pages';
			$this->pageData['to'] = $redir_to;
                        
			render_php_template( $this->settings['template'], $this->pageData );


		} else {

			$this->pageData['content'] = pullContent( AUTH_ROOT.'/pages/login' );

			$this->pageData['id'] = 'login';
			$this->pageData['fail'] = False;
			$this->pageData['title'] = "Naterweb Authentication";
			$this->pageData['static'] = AUTH_ROOT.'/pages';

			render_php_template( $this->settings['template'], $this->pageData );

		}



	}

	/** 
	 * Renders the group management page where groups can 
	 * be added.
	 */
	public function managegroup(){
            

		if ( auth_user( getConfigOption('site_domain').'/?url=auth/managegroup', 'nwadmin' ) ){
            
            $sess = SessionMgr::getInstance();
            $sess->noRedirect = True;
            $pageData = array();
            $pageData['groups'] = $this->dal->getAll( 'nwGroup' );
            $pageData['content'] = pullContent( AUTH_ROOT.'/pages/managegroup');
            $pageData['static'] = AUTH_ROOT.'/pages';
            $pageData['title'] = 'Manage Groups';

            render_php_template( $this->settings['template'], $pageData );
        } else {

            $this->unauthorized();
        }



	}

	/**
	 * Renders the add group page where a group can be added.
	 */
	public function addgroup(){

		if ( ! auth_user( getConfigOption('site_domain').'/?url=auth/adduser', 'nwadmin' ) ){
            $this->unauthorized();
        }

		$sessionmgr = SessionMgr::getInstance();
        $sessionmgr->noRedirect = True;


		if ( $sessionmgr->check_csrf('post') && 
			! $this->dal->get('nwGroup', 'name', request::sanitized_post('name') ) ){



			$newgroup = new nwGroup();

			$newgroup->name = request::sanitized_post('name');

			$newgroup->save();


			$this->pageData['content'] = pullContent( AUTH_ROOT.'/pages/groupadded' );

			$this->pageData['id'] = 'login';
			$this->pageData['title'] = "Naterweb Authentication";
			$this->pageData['static'] = AUTH_ROOT.'/pages';

			render_php_template( $this->settings['template'], $this->pageData );


		} else {

			$pageData = array();
			$pageData['content'] = pullContent( AUTH_ROOT.'/pages/addgroup' );
			$pageData['static'] = AUTH_ROOT.'/pages';
			$pageData['title'] = 'Add New User';
			$pageData['id'] = 'adduser';

			render_php_template( $this->settings['template'], $pageData );


		}


	}

	/**
	 * Deletes a user from the database along with 
	 * their group associations.
	 */
	public function deluser(){

		if ( ! auth_user( getConfigOption('site_domain').'/?url=auth/manage', 'nwadmin' ) ){
                    $this->unauthorized();
                }

		$sessionmgr = SessionMgr::getInstance();
                $sessionmgr->noRedirect = True;

		$user = $this->dal->get( 'nwUser', 'id', request::sanitized_get( 'uid') );

		foreach ($user->getRelated( 'groups' ) as $g) {
			$user->removeRelated( 'groups', $g );
		}

		$user->delete();

		$redir = new Redirect( 'auth/deluser', '/?url=auth/manage');
		$redir->apply(302);




	}

	public function changegroup(){
		print 'not implemented';
	}


	/** 
	 * Updates a user's information and groups
	 */
	public function changeuser(){

		if ( ! auth_user( getConfigOption('site_domain').'/?url=auth/adduser', 'nwadmin' ) ){
                    $this->unauthorized();
                }

		$sessionmgr = SessionMgr::getInstance();
                $sessionmgr->noRedirect = True;


		if ( $sessionmgr->check_csrf('post') && 
			  $this->dal->get('nwUser', 'id', request::sanitized_post('uid') ) ){


{'fraction_non_alphanumeric': 0.177471399237313, 'fraction_numerical': 0.0007822430820377432, 'mean_word_length': 2.617969579059073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '798828', 'n_tokens_mistral': 3539, 'n_tokens_neox': 3124, 'n_words': 552}