Skip to content

Instantly share code, notes, and snippets.

@ddellspe
Created February 20, 2016 18:38
Show Gist options
  • Select an option

  • Save ddellspe/2bea3dbdc8b21b31df82 to your computer and use it in GitHub Desktop.

Select an option

Save ddellspe/2bea3dbdc8b21b31df82 to your computer and use it in GitHub Desktop.
public function login(){
if(isset($this->params->query['uip_ticket'])){
$uip_ticket = $this->params->query['uip_ticket'];
$hawkid = "ddellspe";
$url = "https://login.uiowa.edu/uip/checkticket.page?service=https://www.engineering.uiowa.edu/officehours/eps2/users/login&uip_ticket=".$uip_ticket;
$rsp = file_get_contents($url);
$rsp = str_replace("\n",'&',$rsp);
parse_str($rsp);
if(!isset($error)){
if($user = $this->User->find('first', array('conditions' => array('hawkid' => $hawkid)))) {
if($this->Auth->login($user)){
$this->Session->setFlash(__('Successfully Logged Into the site'),'flash_success');
$this->User->id = $user['User']['id'];
$this->User->saveField('last_login', date("Y-m-d H:i:s"));
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Failed to authenticate the user, please contact the webmaster if this occurrs again.'),'flash_error');
$this->redirect('/');
}
}else{
$fName = $hawkid;
$lName = " ";
//get first/lastname
$ldapconn = ldap_connect("directory.iowa.uiowa.edu", 389);
$ldapbind = ldap_bind($ldapconn);
$dn = 'ou=people,dc=metadirectory,dc=iowa,dc=uiowa,dc=edu';
$searchResult = ldap_search($ldapconn, $dn, '(|(uiowahawkid='.$hawkid.'))') or die(ldap_error($ldapconn));
if(ldap_count_entries($ldapconn, $searchResult) > 0){
$info = ldap_get_entries($ldapconn, $searchResult) or die(ldap_error($ldapconn));
for($i=0 ; $i<$info['count'] ; $i++){
$fName = $info[$i]['givenname'][0];
$lName = $info[$i]['sn'][0];
}
} else {
$fName = $hawkid;
$lName = " ";
}
//add into users table
$user = $this->User->create();
$user['User']['hawkid'] = $hawkid;
$user['User']['first_name'] = $fName;
$user['User']['last_name'] = $lName;
$user['USer']['password'] = null;
$user['User']['group_id'] = 3;
$this->User->save($user);
if($user = $this->User->find('first', array('conditions' => array('hawkid' => $hawkid)))) {
if($this->Auth->login($user)){
$this->Session->setFlash(__('Successfully Logged Into the site'),'flash_success');
$this->User->id = $user['User']['id'];
$this->User->saveField('last_login', date("Y-m-d H:i:s"));
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Failed to authenticate the user, please contact the webmaster if this occurrs again.'),'flash_error');
$this->redirect('/');
}
} else {
$this->Session->setFlash(__('Failed to log you into the system after user creation, please try logging in again'),'flash_error');
$this->redirect('/');
}
}
} else {
$this->Session->setFlash(__('Failed to authenticate your hawkid with the system'),'flash_error');
$this->redirect('/');
}
} else {
$this->redirect('/');
}
}
Router::redirect('/login', 'https://login.uiowa.edu/uip/login.page?service=https://www.engineering.uiowa.edu/officehours/eps2/users/login');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment