diff --git a/application/config/aauth.php b/application/config/aauth.php index 6f6ec8f..34c4c1b 100644 --- a/application/config/aauth.php +++ b/application/config/aauth.php @@ -33,9 +33,14 @@ $config['aauth'] = array( 'perms' => 'aauth_perms', // perms to group 'perm_to_group' => 'aauth_perm_to_group', + // perms to group + 'perm_to_user' => 'aauth_perm_to_user', // pm table 'pms' => 'aauth_pms', - + // system variables + 'system_variables' => 'aauth_system_variables', + // user variables + 'user_variables' => 'aauth_user_variables', // remember time 'remember' => ' +3 days', @@ -46,12 +51,13 @@ $config['aauth'] = array( // non alphanumeric characters that are allowed in a name 'valid_chars' => array(' ', '\''), - // it limits login attempts - 'dos_protection' => true, + // ddos protection, + //if it is true, the user will be banned temporary when he exceed the login 'try' + 'ddos_protection' => true, // login attempts time interval - // default 10 times in one minute - 'try' => 10, + // default 20 times in one hour + 'max_login_attempt' => 10, // to register email verifitaion need? true / false 'verification' => false, @@ -85,6 +91,7 @@ $config['aauth'] = array( 'no_user' => 'User not Exist', 'not_verified' => 'Please verify your account.', 'group_exist' => 'Group already exists', + 'no_group' => 'Group doesn\'t exists', 'self_pm' => 'It is not reasonable to send pm to yourself :)', 'no_pm' => 'Pm not found', diff --git a/application/controllers/example.php b/application/controllers/example.php index b34f913..e4a462b 100644 --- a/application/controllers/example.php +++ b/application/controllers/example.php @@ -19,10 +19,13 @@ class Example extends CI_Controller { public function index() { - if ($this->aauth->login('admin@admin.com', 'password', true)) + if ($this->aauth->login('aa@a.com', '12345')) echo 'tmm'; - + else + echo 'hyr'; //echo date("Y-m-d H:i:s"); + + $this->aauth->print_errors(); } function debug(){ @@ -31,14 +34,14 @@ class Example extends CI_Controller { print_r( //$this->aauth->is_admin() - //$this->aauth->get_user() - //$this->aauth->control_group("Mod") - //$this->aauth->control_perm(1) - //$this->aauth->list_groups() - //$this->aauth->list_users() - //$this->aauth->is_allowed(1) - //$this->aauth->is_admin() - //$this->aauth->create_perm("deneme",'defff') + //$this->aauth->get_user() + //$this->aauth->control_group("Mod") + //$this->aauth->control_perm(1) + //$this->aauth->list_groups() + //$this->aauth->list_users() + //$this->aauth->is_allowed(1) + //$this->aauth->is_admin() + //$this->aauth->create_perm("deneme",'defff') //$this->aauth->update_perm(3,'dess','asd') //$this->aauth->allow(1,1) //$this->aauth->add_member(1,1) @@ -94,11 +97,16 @@ class Example extends CI_Controller { //$this->aauth->_reset_login_attempts(1); } + public function login_fast(){ + $this->aauth->login_fast(1); + } public function is_loggedin() { if ($this->aauth->is_loggedin()) echo 'girdin'; + + print_r( $this->aauth->get_user() ); } public function logout() { @@ -108,7 +116,7 @@ class Example extends CI_Controller { public function is_member() { - if ($this->aauth->is_member('Admin')) + if ($this->aauth->is_member('deneme',9)) echo 'uye'; } @@ -127,14 +135,19 @@ class Example extends CI_Controller { } } - public function group() { + public function get_group_name() { + + echo $this->aauth->get_group_name(1); + } + + public function get_group_id() { echo $this->aauth->get_group_id("Admin"); } public function list_users() { echo '
';
-        print_r($this->aauth->list_users("Mod"));
+        print_r($this->aauth->list_users());
         echo '
'; } @@ -146,24 +159,31 @@ class Example extends CI_Controller { public function check_email() { - if ($this->aauth->check_email("emre@emreakay.com")) + if ($this->aauth->check_email("aa@a.com")) echo 'uygun '; else echo 'alindi '; - echo $this->aauth->get_errors(); - - echo ' sadsad'; + $this->aauth->print_errors(); } public function get_user() { - print_r($this->aauth->get_user(1)); + print_r($this->aauth->get_user()); } function create_user() { - $a = $this->aauth->create_user("ess@as.com", "asd", "asdasd"); + + $a = $this->aauth->create_user("admin@admin.com", "12345", "Admin"); + + if ($a) + echo "tmm "; + else + echo "hyr "; + print_r($this->aauth->get_user($a)); + + $this->aauth->print_errors(); } public function is_banned() { @@ -177,39 +197,180 @@ class Example extends CI_Controller { print_r($a); } + function delete_user() { + + $a = $this->aauth->delete_user(7); + + print_r($a); + } + + function unban_user() { + + $a = $this->aauth->unban_user(6); + + print_r($a); + } + function update_user() { - $a = $this->aauth->update_user(3, "xxx@ssdas.com", "asd", "asdasd"); + $a = $this->aauth->update_user(6, "a@a.com", "12345", "tested"); + + print_r($a); + } + + function update_activity() { + $a = $this->aauth->update_activity(); + + print_r($a); + } + + function update_login_attempt() { + $a = $this->aauth->update_login_attempts("a@a.com"); print_r($a); } function create_group() { - $a = $this->aauth->create_group("denemeee"); + $a = $this->aauth->create_group("deneme"); } function delete_group() { - $a = $this->aauth->delete_group(3); + $a = $this->aauth->delete_group("deneme"); } function update_group() { - $a = $this->aauth->update_group(4, "zxxx"); + $a = $this->aauth->update_group("deneme", "zxxx"); } function add_member() { - $a = $this->aauth->add_member(1, 4); + $a = $this->aauth->add_member(8, "deneme"); } function fire_member() { - $a = $this->aauth->fire_member(1, 4); + $a = $this->aauth->fire_member(8, "deneme"); + } + + + function create_perm() { + + $a = $this->aauth->create_perm("deneme","def"); + } + + + function update_perm() { + + $a = $this->aauth->update_perm("deneme","deneme","xxx"); + } + + function delete_perm() { + + $a = $this->aauth->update_perm("deneme","deneme","xxx"); + } + + function allow_user() { + + $a = $this->aauth->allow_user(9,"deneme"); + } + + + function deny_user() { + + $a = $this->aauth->deny_user(9,"deneme"); + } + + function allow_group() { + + $a = $this->aauth->allow_group("deneme","deneme"); + } + + function deny_group() { + + $a = $this->aauth->deny_group("deneme","deneme"); + } + + function list_perms() { + + $a = $this->aauth->list_perms(); + print_r($a); + } + + function get_perm_id() { + + $a = $this->aauth->get_perm_id("deneme"); + print_r($a); + } + + + function send_pm() { + + $a = $this->aauth->send_pm(1,8,'s',"w"); + $this->aauth->print_errors(); + } + + function list_pms(){ + + print_r( $this->aauth->list_pms() ); + } + + function get_pm(){ + + print_r( $this->aauth->get_pm(39,false)); + } + + function delete_pm(){ + + $this->aauth->delete_pm(41); + } + + + function count_unread_pms(){ + + echo $this->aauth->count_unread_pms(8); + } + + function error(){ + + $this->aauth->error("asd"); + $this->aauth->error("xasd"); + $this->aauth->keep_errors(); + $this->aauth->print_errors(); + + } + + function keep_errors(){ + + $this->aauth->print_errors(); + //$this->aauth->keep_errors(); + } + + function set_user_var(){ + $this->aauth->set_user_var("emre","akasy"); + } + + function unset_user_var(){ + $this->aauth->unset_user_var("emre"); + } + + function get_user_var(){ + echo $this->aauth->get_user_var("emre"); + } + + function set_system_var(){ + $this->aauth->set_system_var("emre","akay"); + } + + function unset_system_var(){ + $this->aauth->unset_system_var("emre"); + } + + function get_system_var(){ + echo $this->aauth->get_system_var("emre"); } - - -} +}//end /* End of file welcome.php */ diff --git a/application/libraries/Aauth.php b/application/libraries/Aauth.php index d48383f..37634d8 100644 --- a/application/libraries/Aauth.php +++ b/application/libraries/Aauth.php @@ -6,18 +6,21 @@ * Despite ease of use, it has also very advanced features like private messages, * groupping, access management, public access etc.. * - * @author Emre Akay + * @author Emre Akay * @contributor Jacob Tomlinson * * @copyright 2014 Emre Akay * - * @version 1.0 + * @version 2.0 * * @license LGPL * @license http://opensource.org/licenses/LGPL-3.0 Lesser GNU Public License * * The latest version of Aauth can be obtained from: * https://github.com/emreakay/CodeIgniter-Aauth + * + * + * */ class Aauth { @@ -54,9 +57,7 @@ class Aauth { */ public function __construct() { - // Delete all errors at first - $this->errors = array(); - + // get main CI object $this->CI = & get_instance(); // Dependancies @@ -73,22 +74,12 @@ class Aauth { $this->config_vars = & $this->CI->config->item('aauth'); } - /** - * Hash password - * Hash the password for storage in the database - * @param string $pass Password to hash - * @return string Hashed password - */ - function hash_password($pass, $userid) { - - $salt = md5($userid); - return hash('sha256', $salt.$pass); - } ######################## - # User Functions + # Login Functions ######################## + //tested /** * Login user * Check provided details against the database. Add items to error array on fail, create session if success @@ -109,24 +100,28 @@ class Aauth { $this->CI->input->set_cookie($cookie); - if( !valid_email($email) or !ctype_alnum($pass) or strlen($pass) < 5 or strlen($pass) > $this->config_vars['max'] ) { + // verification + if( !valid_email($email) or !ctype_alnum($pass) or strlen($pass) < 5 or + strlen($pass) > $this->config_vars['max'] ) + { $this->error($this->config_vars['wrong']); - return false;} + return false; + } + + $query = null; $query = $this->CI->db->where('email', $email); $query = $this->CI->db->get($this->config_vars['users']); + $row = $query->row(); - $user_id = $query->row()->id; - - if ($query->num_rows() > 0) { - $row = $query->row(); + // only email found and login attempts exceeded + if ($query->num_rows() > 0 and $this->config_vars['ddos_protection'] and ! $this->update_login_attempts($row->email)) { - if ( $this->config_vars['dos_protection'] and $row->last_login_attempt != '' and (strtotime("now") + 30 * $this->config_vars['try'] ) < strtotime($row->last_login_attempt) ) { - $this->error($this->config_vars['exceeded']); - return false; - } + $this->error($this->config_vars['exceeded']); + return false; } + // if user is not verified $query = null; $query = $this->CI->db->where('email', $email); $query = $this->CI->db->where('banned', 1); @@ -138,17 +133,25 @@ class Aauth { return false; } + // to find user id, create sessions and cookies + $query = $this->CI->db->where('email', $email); + $query = $this->CI->db->get($this->config_vars['users']); + + $user_id = $query->row()->id; + $query = null; $query = $this->CI->db->where('email', $email); // Database stores pasword hashed password $query = $this->CI->db->where('pass', $this->hash_password($pass, $user_id)); $query = $this->CI->db->where('banned', 0); + $query = $this->CI->db->get($this->config_vars['users']); $row = $query->row(); - if ($query->num_rows() > 0) { + // if email and pass matches and not banned + if ( $query->num_rows() > 0 ) { // If email and pass matches // create session @@ -161,8 +164,8 @@ class Aauth { $this->CI->session->set_userdata($data); - // id remember selected - if ($remember){ + // if remember selected + if ( $remember ){ $expire = $this->config_vars['remember']; $today = date("Y-m-d"); $remember_date = date("Y-m-d", strtotime($today . $expire) ); @@ -184,38 +187,16 @@ class Aauth { $this->update_activity(); return TRUE; - - } else { - - $query = $this->CI->db->where('email', $email); - $query = $this->CI->db->get($this->config_vars['users']); - $row = $query->row(); - - if ($query->num_rows() > 0) { - - if ( $row->last_login_attempt == null or (strtotime("now") - 600) > strtotime($row->last_login_attempt) ) - { - $data = array( - 'last_login_attempt' => date("Y-m-d H:i:s") - ); - - } else if (!($row->last_login_attempt != '' and (strtotime("now") + 30 * $this->config_vars['try'] ) < strtotime($row->last_login_attempt))) { - - $newtimestamp = strtotime("$row->last_login_attempt + 30 seconds"); - $data = array( - 'last_login_attempt' => date( 'Y-m-d H:i:s', $newtimestamp ) - ); - } - - $query = $this->CI->db->where('email', $email); - $this->CI->db->update($this->config_vars['users'], $data); - } + } + // if not matches + else { $this->error($this->config_vars['wrong']); return FALSE; } } + //tested /** * Check user login * Checks if user logged in, also checks remember. @@ -223,13 +204,14 @@ class Aauth { */ public function is_loggedin() { - if($this->CI->session->userdata('loggedin')) - {return true;} + if ( $this->CI->session->userdata('loggedin') ) + { return true; } - else{ - if( !$this->CI->input->cookie('user', TRUE) ){ + // cookie control + else { + if( ! $this->CI->input->cookie('user', TRUE) ){ return false; - }else{ + } else { $cookie = explode('-', $this->CI->input->cookie('user', TRUE)); if(!is_numeric( $cookie[0] ) or strlen($cookie[1]) < 13 ){return false;} else{ @@ -257,86 +239,49 @@ class Aauth { } } + return false; } /** - * Controls if a logged or public user has permiision + * Controls if a logged or public user has permission * If no permission, it stops script, it also updates last activity every time function called * @param bool $perm_par If not given just control user logged in or not */ - public function control($perm_par = false){ - - if(!$perm_par and !$this->is_loggedin()){ - echo $this->config_vars['no_access']; - die(); - } + public function control( $perm_par ){ $perm_id = $this->get_perm_id($perm_par); $this->update_activity(); - if( !$this->is_allowed($perm_id) ) { + // if user or user's group not allowed + if ( ! $this->is_allowed($perm_id) or ! $this->is_group_allowed($perm_id) ){ echo $this->config_vars['no_access']; die(); } + } + //tested /** * Logout user - * Destroys the CodeIgniter session to log out user. + * Destroys the CodeIgniter session and remove cookies to log out user. * @return bool If session destroy successful */ public function logout() { - return $this->CI->session->sess_destroy(); - } - - /** - * List users - * Return users as an object array - * @param bool|int $group_par Specify group id to list group or false for all users - * @param string $limit Limit of users to be returned - * @param bool $offset Offset for limited number of users - * @param bool $include_banneds Include banned users - * @return array Array of users - */ - public function list_users($group_par = FALSE, $limit = FALSE, $offset = FALSE, $include_banneds = FALSE) { - - // if group_par is given - if ($group_par != FALSE) { - - $group_par = $this->get_group_id($group_par); - $this->CI->db->select('*') - ->from($this->config_vars['users']) - ->join($this->config_vars['user_to_group'], $this->config_vars['users'] . ".id = " . $this->config_vars['user_to_group'] . ".user_id") - ->where($this->config_vars['user_to_group'] . ".group_id", $group_par); - - // if group_par is not given, lists all users - } else { - - $this->CI->db->select('*') - ->from($this->config_vars['users']); - } - - // banneds - if (!$include_banneds) { - $this->CI->db->where('banned != ', 1); - } - - // limit - if ($limit) { - - if ($offset == FALSE) - $this->CI->db->limit($limit); - else - $this->CI->db->limit($limit, $offset); - } + $cookie = array( + 'name' => 'user', + 'value' => '', + 'expire' => time()-3600, + 'path' => '/', + ); - $query = $this->CI->db->get(); + $this->CI->input->set_cookie($cookie); - return $query->result(); + return $this->CI->session->sess_destroy(); } + //tested /** * Fast login * Login with just a user id @@ -366,108 +311,26 @@ class Aauth { } /** - * Create user - * Creates a new user - * @param string $email User's email address - * @param string $pass User's password - * @param string $name User's name - * @return int|bool False if create fails or returns user id if successful - */ - public function create_user($email, $pass, $name='') { - - $valid = true; - - if (!$this->check_email($email)) { - $this->error($this->config_vars['email_taken']); - $valid = false; - } - if (!valid_email($email)){ - $this->error($this->config_vars['email_invalid']); - $valid = false; - } - if (strlen($pass) < 5 or strlen($pass) > $this->config_vars['max'] ){ - $this->error($this->config_vars['pass_invalid']); - $valid = false; - } - if ($name !='' and !ctype_alnum(str_replace($this->config_vars['valid_chars'], '', $name))){ - $this->error($this->config_vars['name_invalid']); - $valid = false; - } - - if (!$valid) { return false; } - - $data = array( - 'email' => $email, - 'pass' => $this->hash_password($pass, 0), // Password cannot be blank but user_id required for salt, setting bad password for now - 'name' => $name, - ); - - if ( $this->CI->db->insert($this->config_vars['users'], $data )){ - - $user_id = $this->CI->db->insert_id(); - - // set default group - $this->add_member($user_id, $this->config_vars['default_group']); - - if($this->config_vars['verification']){ - $data = null; - $data['banned'] = 1; - - $this->CI->db->where('id', $user_id); - $this->CI->db->update($this->config_vars['users'], $data); - $this->send_verification($user_id); - } - - // Update to correct salted password - $data = null; - $data['pass'] = $this->hash_password($pass, $user_id); - $this->CI->db->where('id', $user_id); - $this->CI->db->update($this->config_vars['users'], $data); - - return $user_id; - - } else { - return FALSE; - } - } - - /** - * Update user - * Updates existing user details - * @param int $user_id User id to update - * @param string|bool $email User's email address, or false if not to be updated - * @param string|bool $pass User's password, or false if not to be updated - * @param string|bool $name User's name, or false if not to be updated - * @return bool Update fails/succeeds + * Reset last login attempts + * Sets a users 'last login attempts' to null + * @param int $user_id User id to reset + * @return bool Reset fails/succeeds */ - public function update_user($user_id, $email = FALSE, $pass = FALSE, $name = FALSE) { - - $data = array(); - - if ($email != FALSE) { - $data['email'] = $email; - } - - if ($pass != FALSE) { - $data['pass'] = $this->hash_password($pass, $user_id); - } - - if ($name != FALSE) { - $data['name'] = $name; - } + public function reset_login_attempts($user_id) { + $data['last_login_attempts'] = null; $this->CI->db->where('id', $user_id); return $this->CI->db->update($this->config_vars['users'], $data); } /** - * Send verification email - * Sends a verification email based on user id - * @param int $user_id User id to send verification email to + * Remind password + * Emails user with link to reset password + * @param string $email Email for account to remind */ - public function send_verification($user_id){ + public function remind_password($email){ - $query = $this->CI->db->where( 'id', $user_id ); + $query = $this->CI->db->where( 'email', $email ); $query = $this->CI->db->get( $this->config_vars['users'] ); if ($query->num_rows() > 0){ @@ -477,293 +340,463 @@ class Aauth { $data['verification_code'] = $ver_code; - $this->CI->db->where('id', $user_id); + $this->CI->db->where('email', $email); $this->CI->db->update($this->config_vars['users'], $data); $this->CI->email->from( $this->config_vars['email'], $this->config_vars['name']); $this->CI->email->to($row->email); - $this->CI->email->subject($this->config_vars['email']); - $this->CI->email->message($this->config_vars['code'] . $ver_code . - $this->config_vars['link'] . $user_id . '/' . $ver_code ); + $this->CI->email->subject($this->config_vars['reset']); + $this->CI->email->message($this->config_vars['remind'] . ' ' . + $this->config_vars['remind'] . $row->id . '/' . $ver_code ); $this->CI->email->send(); } } /** - * Verify user - * Activates user account based on verification code - * @param int $user_id User id to activate - * @param string $ver_code Code to validate against - * @return bool Activation fails/succeeds + * Reset password + * Generate new password and email it to the user + * @param int $user_id User id to reset password for + * @param string $ver_code Verification code for account + * @return bool Password reset fails/succeeds */ - public function verify_user($user_id, $ver_code){ + public function reset_password($user_id, $ver_code){ $query = $this->CI->db->where('id', $user_id); $query = $this->CI->db->where('verification_code', $ver_code); $query = $this->CI->db->get( $this->config_vars['users'] ); - if( $query->num_rows() >0 ){ + $pass = random_string('alphanum',8); + + if( $query->num_rows() > 0 ){ $data = array( 'verification_code' => '', - 'banned' => 0 + 'pass' => $this->hash_password($pass, $user_id) ); + $row = $query->row(); + $email = $row->email; + $this->CI->db->where('id', $user_id); $this->CI->db->update($this->config_vars['users'] , $data); + + $this->CI->email->from( $this->config_vars['email'], $this->config_vars['name']); + $this->CI->email->to($email); + $this->CI->email->subject($this->config_vars['reset']); + $this->CI->email->message($this->config_vars['new_password'] . $pass); + $this->CI->email->send(); + return true; } + return false; } + //tested /** - * Reset last login attempts - * Sets a users 'last login attempts' to null - * @param int $user_id User id to reset - * @return bool Reset fails/succeeds + * Update last login + * Update user's last login date + * @param int|bool $user_id User id to update or false for current user + * @return bool Update fails/succeeds */ - public function reset_login_attempts($user_id) { + public function update_last_login($user_id = FALSE) { + + if ($user_id == FALSE) + $user_id = $this->CI->session->userdata('id'); + + $data['last_login'] = date("Y-m-d H:i:s"); + $data['ip_address'] = $this->CI->input->ip_address(); - $data['last_login_attempts'] = null; $this->CI->db->where('id', $user_id); return $this->CI->db->update($this->config_vars['users'], $data); } + + //tested /** - * Ban user - * Bans a user account - * @param int $user_id User id to ban - * @return bool Ban fails/succeeds + * Update login attempt and if exceeds return false + * Update user's last login attemp date and number date + * @param string $email User email + * @return bool */ - public function ban_user($user_id) { + public function update_login_attempts($email) { - $data = array( - 'banned' => 1 - ); + $user_id = $this->get_user_id($email); - $this->CI->db->where('id', $user_id); + $query = $this->CI->db->where('id', $user_id); + $query = $this->CI->db->get( $this->config_vars['users'] ); + $row = $query->row(); + + + $data = array(); + + if ( strtotime($row->last_login_attempt) == strtotime(date("Y-m-d H:0:0"))) { + + $data['login_attempts'] = $row->login_attempts + 1; + + $query = $this->CI->db->where('id', $user_id); + $this->CI->db->update($this->config_vars['users'], $data); + + } else { + + $data['last_login_attempt'] = date("Y-m-d H:0:0"); + $data['login_attempts'] = 1; + + $this->CI->db->where('id', $user_id); + $this->CI->db->update($this->config_vars['users'], $data); + + } + + if ( $data['login_attempts'] > $this->config_vars['max_login_attempt'] ) { + return false; + } else { + return true; + } - return $this->CI->db->update($this->config_vars['users'], $data); } /** - * Unban user - * Activates user account - * @param int $user_id User id to activate - * @return bool Activation fails/succeeds + * Update remember + * Update amount of time a user is remembered for + * @param int $user_id User id to update + * @param int $expression + * @param int $expire + * @return bool Update fails/succeeds */ - public function unlock_user($user_id) { - - $data = array( - 'banned' => 0 - ); + public function update_remember($user_id, $expression=null, $expire=null) { - $this->CI->db->where('id', $user_id); + $data['remember_time'] = $expire; + $data['remember_exp'] = $expression; + $query = $this->CI->db->where('id',$user_id); return $this->CI->db->update($this->config_vars['users'], $data); } + + ######################## + # User Functions + ######################## + + //tested /** - * Check user banned - * Checks if a user is banned - * @param int $user_id User id to check - * @return bool Flase if banned, True if not + * Create user + * Creates a new user + * @param string $email User's email address + * @param string $pass User's password + * @param string $name User's name + * @return int|bool False if create fails or returns user id if successful */ - public function is_banned($user_id) { + public function create_user($email, $pass, $name='') { - $query = $this->CI->db->where('id', $user_id); - $query = $this->CI->db->where('banned', 1); + $valid = true; - $query = $this->CI->db->get($this->config_vars['users']); + // if email is already exist + if ( ! $this->check_email($email)) { + $this->error($this->config_vars['email_taken']); + $valid = false; + } - if ($query->num_rows() > 0) - return TRUE; - else + if ( ! valid_email($email)){ + $this->error($this->config_vars['email_invalid']); + $valid = false; + } + if ( strlen($pass) < 5 or strlen($pass) > $this->config_vars['max'] ){ + $this->error($this->config_vars['pass_invalid']); + $valid = false; + } + if ($name !='' and !ctype_alnum(str_replace($this->config_vars['valid_chars'], '', $name))){ + $this->error($this->config_vars['name_invalid']); + $valid = false; + } + + if (!$valid) { + return false; } + + $data = array( + 'email' => $email, + 'pass' => $this->hash_password($pass, 0), // Password cannot be blank but user_id required for salt, setting bad password for now + 'name' => $name, + ); + + if ( $this->CI->db->insert($this->config_vars['users'], $data )){ + + $user_id = $this->CI->db->insert_id(); + + // set default group + $this->add_member($user_id, $this->config_vars['default_group']); + + // if verification activated + if($this->config_vars['verification']){ + $data = null; + $data['banned'] = 1; + + $this->CI->db->where('id', $user_id); + $this->CI->db->update($this->config_vars['users'], $data); + + // sends verifition ( !! e-mail settings must be set) + $this->send_verification($user_id); + } + + // Update to correct salted password + $data = null; + $data['pass'] = $this->hash_password($pass, $user_id); + $this->CI->db->where('id', $user_id); + $this->CI->db->update($this->config_vars['users'], $data); + + return $user_id; + + } else { return FALSE; + } } + //tested /** - * Delete user - * Delete a user from database. WARNING Can't be undone - * @param int $user_id User id to delete + * Update user + * Updates existing user details + * @param int $user_id User id to update + * @param string|bool $email User's email address, or false if not to be updated + * @param string|bool $pass User's password, or false if not to be updated + * @param string|bool $name User's name, or false if not to be updated + * @return bool Update fails/succeeds */ - public function delete_user($user_id) { + public function update_user($user_id, $email = FALSE, $pass = FALSE, $name = FALSE) { + + $data = array(); + + if ($email != FALSE) { + $data['email'] = $email; + } + + if ($pass != FALSE) { + $data['pass'] = $this->hash_password($pass, $user_id); + } + + if ($name != FALSE) { + $data['name'] = $name; + } $this->CI->db->where('id', $user_id); - $this->CI->db->delete($this->config_vars['users']); + return $this->CI->db->update($this->config_vars['users'], $data); } + //tested /** - * Check email - * Checks if an email address is available - * @param string $email Email to check - * @return bool True if available, False if not + * List users + * Return users as an object array + * @param bool|int $group_par Specify group id to list group or false for all users + * @param string $limit Limit of users to be returned + * @param bool $offset Offset for limited number of users + * @param bool $include_banneds Include banned users + * @return array Array of users */ - public function check_email($email) { + public function list_users($group_par = FALSE, $limit = FALSE, $offset = FALSE, $include_banneds = FALSE) { - $this->CI->db->where("email", $email); - $query = $this->CI->db->get($this->config_vars['users']); + // if group_par is given + if ($group_par != FALSE) { - if ($query->num_rows() > 0) { - $this->info($this->config_vars['email_taken']); - return FALSE; + $group_par = $this->get_group_id($group_par); + $this->CI->db->select('*') + ->from($this->config_vars['users']) + ->join($this->config_vars['user_to_group'], $this->config_vars['users'] . ".id = " . $this->config_vars['user_to_group'] . ".user_id") + ->where($this->config_vars['user_to_group'] . ".group_id", $group_par); + + // if group_par is not given, lists all users + } else { + + $this->CI->db->select('*') + ->from($this->config_vars['users']); } - else - return TRUE; - } - /** - * Remind password - * Emails user with link to reset password - * @param string $email Email for account to remind - */ - public function remind_password($email){ + // banneds + if (!$include_banneds) { + $this->CI->db->where('banned != ', 1); + } - $query = $this->CI->db->where( 'email', $email ); - $query = $this->CI->db->get( $this->config_vars['users'] ); + // limit + if ($limit) { - if ($query->num_rows() > 0){ - $row = $query->row(); + if ($offset == FALSE) + $this->CI->db->limit($limit); + else + $this->CI->db->limit($limit, $offset); + } - $ver_code = random_string('alnum', 16); + $query = $this->CI->db->get(); - $data['verification_code'] = $ver_code; + return $query->result(); + } - $this->CI->db->where('email', $email); - $this->CI->db->update($this->config_vars['users'], $data); + //tested + /** + * Get user + * Get user information + * @param int|bool $user_id User id to get or false for current user + * @return object User information + */ + public function get_user($user_id = FALSE) { - $this->CI->email->from( $this->config_vars['email'], $this->config_vars['name']); - $this->CI->email->to($row->email); - $this->CI->email->subject($this->config_vars['reset']); - $this->CI->email->message($this->config_vars['remind'] . ' ' . - $this->config_vars['remind'] . $row->id . '/' . $ver_code ); - $this->CI->email->send(); + if ($user_id == FALSE) + $user_id = $this->CI->session->userdata('id'); + + $query = $this->CI->db->where('id', $user_id); + $query = $this->CI->db->get($this->config_vars['users']); + + if ($query->num_rows() <= 0){ + $this->error($this->config_vars['no_user']); + return FALSE; } + return $query->row(); } /** - * Reset password - * Generate new password and email it to the user - * @param int $user_id User id to reset password for - * @param string $ver_code Verification code for account - * @return bool Password reset fails/succeeds + * Verify user + * Activates user account based on verification code + * @param int $user_id User id to activate + * @param string $ver_code Code to validate against + * @return bool Activation fails/succeeds */ - public function reset_password($user_id, $ver_code){ + public function verify_user($user_id, $ver_code){ $query = $this->CI->db->where('id', $user_id); $query = $this->CI->db->where('verification_code', $ver_code); $query = $this->CI->db->get( $this->config_vars['users'] ); - $pass = random_string('alphanum',8); - + // if ver code is true if( $query->num_rows() > 0 ){ $data = array( 'verification_code' => '', - 'pass' => $this->hash_password($pass, $user_id) + 'banned' => 0 ); + $this->CI->db->where('id', $user_id); + $this->CI->db->update($this->config_vars['users'] , $data); + return true; + } + return false; + } + + /** + * Send verification email + * Sends a verification email based on user id + * @param int $user_id User id to send verification email to + */ + public function send_verification($user_id){ + + $query = $this->CI->db->where( 'id', $user_id ); + $query = $this->CI->db->get( $this->config_vars['users'] ); + + if ($query->num_rows() > 0){ $row = $query->row(); - $email = $row->email; + + $ver_code = random_string('alnum', 16); + + $data['verification_code'] = $ver_code; $this->CI->db->where('id', $user_id); - $this->CI->db->update($this->config_vars['users'] , $data); + $this->CI->db->update($this->config_vars['users'], $data); $this->CI->email->from( $this->config_vars['email'], $this->config_vars['name']); - $this->CI->email->to($email); - $this->CI->email->subject($this->config_vars['reset']); - $this->CI->email->message($this->config_vars['new_password'] . $pass); + $this->CI->email->to($row->email); + $this->CI->email->subject($this->config_vars['email']); + $this->CI->email->message($this->config_vars['code'] . $ver_code . + $this->config_vars['link'] . $user_id . '/' . $ver_code ); $this->CI->email->send(); - - return true; } - - return false; } + //not tested excatly /** - * Update activity - * Update user's last activity date - * @param int|bool $user_id User id to update or false for current user - * @return bool Update fails/succeeds + * Delete user + * Delete a user from database. WARNING Can't be undone + * @param int $user_id User id to delete */ - public function update_activity($user_id = FALSE) { + public function delete_user($user_id) { - if ($user_id == FALSE) - $user_id = $this->CI->session->userdata('id'); + $this->CI->db->where('id', $user_id); + $this->CI->db->delete($this->config_vars['users']); - if($user_id==false){return false;} + // delete from perm_to_user + $this->CI->db->where('user_id', $user_id); + $this->CI->db->delete($this->config_vars['perm_to_user']); - $data['last_activity'] = date("Y-m-d H:i:s"); + // delete from user_to_group + $this->CI->db->where('user_id', $user_id); + $this->CI->db->delete($this->config_vars['user_to_group']); - $query = $this->CI->db->where('id',$user_id); - return $this->CI->db->update($this->config_vars['users'], $data); + // delete user vars + $this->CI->db->where('user_id', $user_id); + $this->CI->db->delete($this->config_vars['user_variables']); } + //tested /** - * Update last login - * Update user's last login date - * @param int|bool $user_id User id to update or false for current user - * @return bool Update fails/succeeds + * Ban user + * Bans a user account + * @param int $user_id User id to ban + * @return bool Ban fails/succeeds */ - public function update_last_login($user_id = FALSE) { - - if ($user_id == FALSE) - $user_id = $this->CI->session->userdata('id'); + public function ban_user($user_id) { - $data['last_login'] = date("Y-m-d H:i:s"); + $data = array( + 'banned' => 1, + 'verification_code' => '' + ); $this->CI->db->where('id', $user_id); + return $this->CI->db->update($this->config_vars['users'], $data); } + //tested /** - * Update remember - * Update amount of time a user is remembered for - * @param int $user_id User id to update - * @param int $expression - * @param int $expire - * @return bool Update fails/succeeds + * Unban user + * Activates user account + * Same with unlock_user() + * @param int $user_id User id to activate + * @return bool Activation fails/succeeds */ - public function update_remember($user_id, $expression=null, $expire=null) { + public function unban_user($user_id) { - $data['remember_time'] = $expire; - $data['remember_exp'] = $expression; + $data = array( + 'banned' => 0 + ); + + $this->CI->db->where('id', $user_id); - $query = $this->CI->db->where('id',$user_id); return $this->CI->db->update($this->config_vars['users'], $data); } + //tested /** - * Get user - * Get user information - * @param int|bool $user_id User id to get or false for current user - * @return object User information + * Check user banned + * Checks if a user is banned + * @param int $user_id User id to check + * @return bool False if banned, True if not */ - public function get_user($user_id = FALSE) { - - if ($user_id == FALSE) - $user_id = $this->CI->session->userdata('id'); + public function is_banned($user_id) { $query = $this->CI->db->where('id', $user_id); + $query = $this->CI->db->where('banned', 1); + $query = $this->CI->db->get($this->config_vars['users']); - if ($query->num_rows() <= 0){ - $this->error($this->config_vars['no_user']); + if ($query->num_rows() > 0) + return TRUE; + else return FALSE; - } - return $query->row(); } /** * Get user id - * Get user id from email address - * @param string $email Email address for user + * Get user id from email address, if par. not given, return current user's id + * @param string|bool $email Email address for user * @return int User id */ public function get_user_id($email=false) { - if(!$email){ + if( ! $email){ $query = $this->CI->db->where('id', $this->CI->session->userdata('id')); } else { $query = $this->CI->db->where('email', $email); @@ -784,22 +817,78 @@ class Aauth { * @param int|bool $user_id User id to get or false for current user * @return array Groups */ - public function get_user_groups($user_id = false){ - - if ($user_id==false) { $user_id = $this->CI->session->userdata('id'); } - - $this->CI->db->select('*'); - $this->CI->db->from($this->config_vars['user_to_group']); - $this->CI->db->join($this->config_vars['groups'], "id = group_id"); - $this->CI->db->where('user_id', $user_id); + public function get_user_groups($user_id = false){ + + if ($user_id==false) { $user_id = $this->CI->session->userdata('id'); } + + $this->CI->db->select('*'); + $this->CI->db->from($this->config_vars['user_to_group']); + $this->CI->db->join($this->config_vars['groups'], "id = group_id"); + $this->CI->db->where('user_id', $user_id); + + return $query = $this->CI->db->get()->result(); + } + + //tested + /** + * Check email + * Checks if an email address is available + * @param string $email Email to check + * @return bool True if available, False if not + */ + public function check_email($email) { + + $this->CI->db->where("email", $email); + $query = $this->CI->db->get($this->config_vars['users']); + + if ($query->num_rows() > 0) { + $this->info($this->config_vars['email_taken']); + return FALSE; + } + else + return TRUE; + } + + //tested + /** + * Update activity + * Update user's last activity date + * @param int|bool $user_id User id to update or false for current user + * @return bool Update fails/succeeds + */ + public function update_activity($user_id = FALSE) { + + if ($user_id == FALSE) + $user_id = $this->CI->session->userdata('id'); + + if($user_id==false){return false;} + + $data['last_activity'] = date("Y-m-d H:i:s"); + + $query = $this->CI->db->where('id',$user_id); + return $this->CI->db->update($this->config_vars['users'], $data); + } + + //tested + /** + * Hash password + * Hash the password for storage in the database + * (thanks to Jacob Tomlinson for contribution) + * @param string $pass Password to hash + * @param $userid + * @return string Hashed password + */ + function hash_password($pass, $userid) { - return $query = $this->CI->db->get()->result(); + $salt = md5($userid); + return hash('sha256', $salt.$pass); } ######################## # Group Functions ######################## + //tested /** * Create group * Creates a new group @@ -823,6 +912,7 @@ class Aauth { return FALSE; } + //tested /** * Update group * Change a groups name @@ -830,7 +920,9 @@ class Aauth { * @param string $group_name New group name * @return bool Update success/failure */ - public function update_group($group_id, $group_name) { + public function update_group($group_par, $group_name) { + + $group_id = $this->get_group_id($group_par); $data['name'] = $group_name; @@ -838,18 +930,27 @@ class Aauth { return $this->CI->db->update($this->config_vars['groups'], $data); } + //tested /** * Delete group * Delete a group from database. WARNING Can't be undone * @param int $group_id User id to delete * @return bool Delete success/failure */ - public function delete_group($group_id) { + public function delete_group($group_par) { + + $group_id = $this->get_group_id($group_par); + + // bug fixed + // now users are deleted from user_to_group table + $this->CI->db->where('group_id', $group_id); + $this->CI->db->delete($this->config_vars['user_to_group']); $this->CI->db->where('id', $group_id); return $this->CI->db->delete($this->config_vars['groups']); } + //tested /** * Add member * Add a user to a group @@ -859,16 +960,22 @@ class Aauth { */ public function add_member($user_id, $group_par) { - $group_par = $this->get_group_id($group_par); + $group_id = $this->get_group_id($group_par); + + if( ! $group_id ) { + + $this->error( $this->config_vars['group_exist'] ); + return false; + } $query = $this->CI->db->where('user_id',$user_id); - $query = $this->CI->db->where('group_id',$group_par); + $query = $this->CI->db->where('group_id',$group_id); $query = $this->CI->db->get($this->config_vars['user_to_group']); if ($query->num_rows() < 1) { $data = array( 'user_id' => $user_id, - 'group_id' => $group_par + 'group_id' => $group_id ); return $this->CI->db->insert($this->config_vars['user_to_group'], $data); @@ -877,6 +984,7 @@ class Aauth { return true; } + //tested /** * Remove member * Remove a user from a group @@ -884,7 +992,7 @@ class Aauth { * @param int|string $group_par Group id or name to remove user from * @return bool Remove success/failure */ - public function fire_member($user_id, $group_par) { + public function remove_member($user_id, $group_par) { $group_par = $this->get_group_id($group_par); $this->CI->db->where('user_id', $user_id); @@ -892,58 +1000,49 @@ class Aauth { return $this->CI->db->delete($this->config_vars['user_to_group']); } + //tested /** * Is member * Check if current user is a member of a group * @param int|string $group_par Group id or name to check + * @param int|bool $user_id User id, if not given current user * @return bool */ - public function is_member($group_par) { - - $user_id = $this->CI->session->userdata('id'); - - $this->get_group_id($group_par); - // group_id given - if (is_numeric($group_par)) { - - $query = $this->CI->db->where('user_id', $user_id); - $query = $this->CI->db->where('group_id', $group_par); - $query = $this->CI->db->get($this->config_vars['user_to_group']); + public function is_member( $group_par, $user_id = false ) { - $row = $query->row(); - - if ($query->num_rows() > 0) { - return TRUE; - } else { - return FALSE; - } + // if user_id false (not given), current user + if( ! $user_id){ + $user_id = $this->CI->session->userdata('id'); } - // group_name given - else { + $group_id = $this->get_group_id($group_par); - $query = $this->CI->db->where('name', $group_par); - $query = $this->CI->db->get($this->config_vars['groups']); + $query = $this->CI->db->where('user_id', $user_id); + $query = $this->CI->db->where('group_id', $group_id); + $query = $this->CI->db->get($this->config_vars['user_to_group']); - if ($query->num_rows() == 0) - return FALSE; + $row = $query->row(); - $row = $query->row(); - return $this->is_member($row->id); + if ($query->num_rows() > 0) { + return TRUE; + } else { + return FALSE; } } + //tested /** * Is admin * Check if current user is a member of the admin group - * @param int|string $group_par Group id or name to check + * @param int $user_id User id to check, if it is not given checks current user * @return bool */ - public function is_admin() { + public function is_admin( $user_id = false ) { - return $this->is_member($this->config_vars['admin_group']); + return $this->is_member($this->config_vars['admin_group'], $user_id); } + //tested /** * List groups * List all groups @@ -955,6 +1054,8 @@ class Aauth { return $query->result(); } + + //tested /** * Get group name * Get group name from group id @@ -973,13 +1074,14 @@ class Aauth { return $row->name; } + //tested /** * Get group id - * Get group id from group name or id + * Get group id from group name or id ( ! Case sensitive) * @param int|string $group_par Group id or name to get * @return int Group id */ - public function get_group_id($group_par) { + public function get_group_id ( $group_par ) { if( is_numeric($group_par) ) { return $group_par; } @@ -997,6 +1099,7 @@ class Aauth { # Permission Functions ######################## + //tested /** * Create permission * Creates a new permission type @@ -1021,48 +1124,95 @@ class Aauth { return FALSE; } + //tested /** * Update permission * Updates permission name and description - * @param int $perm_id Permission id + * @param int|string $perm_par Permission id or permission name * @param string $perm_name New permission name * @param string $definition Permission description * @return bool Update success/failure */ - public function update_perm($perm_id, $perm_name, $definition=false) { + public function update_perm($perm_par, $perm_name=false, $definition=false) { + + $perm_id = $this->get_perm_id($perm_par); - $data['name'] = $perm_name; + if ($perm_name != false) + $data['name'] = $perm_name; - if ($definition!=false) - $data['definition'] = $perm_name; + if ($definition != false) + $data['definition'] = $definition; $this->CI->db->where('id', $perm_id); return $this->CI->db->update($this->config_vars['perms'], $data); } + //not ok /** * Delete permission * Delete a permission from database. WARNING Can't be undone - * @param int $perm_id Permission id to delete + * @param int|string $perm_par Permission id or perm name to delete * @return bool Delete success/failure */ - public function delete_perm($perm_id) { + public function delete_perm($perm_par) { + + $perm_id = $this->get_perm_id($perm_par); + + // deletes from perm_to_gropup table + $this->CI->db->where('pern_id', $perm_id); + $this->CI->db->delete($this->config_vars['perm_to_group']); + // deletes from perm_to_user table + $this->CI->db->where('pern_id', $perm_id); + $this->CI->db->delete($this->config_vars['perm_to_group']); + + // deletes from permission table $this->CI->db->where('id', $perm_id); return $this->CI->db->delete($this->config_vars['perms']); } /** - * Is allowed + * Is user allowed + * Check if user allowed to do specified action, admin always allowed + * first checks user permissions then check group permissions + * @param int $perm_par Permission id or name to check + * @param int|bool $user_id User id to check, or if false checks current user + * @return bool + */ + public function is_allowed($perm_par, $user_id=false){ + + $perm_id = $this->get_perm_id($perm_par); + + if( $user_id == false){ + $user_id = $this->CI->session->userdata('id'); + } + + $query = $this->CI->db->where('perm_id', $perm_id); + $query = $this->CI->db->where('user_id', $user_id); + $query = $this->CI->db->get( $this->config_vars['perm_to_user'] ); + + if( $query->num_rows() > 0){ + return true; + } elseif ($this->is_group_allowed($perm_id)) { + return true; + } else { + return false; + } + + } + + /** + * Is Group allowed * Check if group is allowed to do specified action, admin always allowed - * @param int|string|bool $group_par Group id or name to check, or if false checks all user groups * @param int $perm_par Permission id or name to check + * @param int|string|bool $group_par Group id or name to check, or if false checks all user groups * @return bool */ - public function is_allowed($group_par=false, $perm_par){ + public function is_group_allowed($perm_par, $group_par=false){ $perm_id = $this->get_perm_id($perm_par); + // if group par is given if($group_par != false){ $group_par = $this->get_group_id($group_par); @@ -1077,76 +1227,125 @@ class Aauth { return false; } } + // if group par is not given + // checks current user's all groups else { - // all doors open to admin :) - if ( $this->is_admin( $this->CI->session->userdata('id')) ) {return true;} - - // if public is allowed - if( !$this->is_loggedin() and $this->is_allowed($perm_id, $this->config_vars['public_group']) ){ - return true; - } + // if public is allowed or he is admin + if ( $this->is_admin( $this->CI->session->userdata('id')) or + $this->is_group_allowed($perm_id, $this->config_vars['public_group']) ) + {return true;} + // if is not login if (!$this->is_loggedin()){return false;} $group_pars = $this->list_groups( $this->CI->session->userdata('id') ); foreach ($group_pars as $g ){ - if($this->is_allowed($perm_id, $g -> id)){ + if($this->is_group_allowed($perm_id, $g -> id)){ return true; } } + return false; + } + } + //tested + /** + * Allow User + * Add User to permission + * @param int $user_id User id to deny + * @param int $perm_par Permission id or name to allow + * @return bool Allow success/failure + */ + public function allow_user($user_id, $perm_par) { - return false; + $perm_id = $this->get_perm_id($perm_par); + + $query = $this->CI->db->where('user_id',$user_id); + $query = $this->CI->db->where('perm_id',$perm_id); + $query = $this->CI->db->get($this->config_vars['perm_to_user']); + + // if not inserted before + if ($query->num_rows() < 1) { + + $data = array( + 'user_id' => $user_id, + 'perm_id' => $perm_id + ); + + return $this->CI->db->insert($this->config_vars['perm_to_user'], $data); } + return true; + } + + //tested + /** + * Deny User + * Remove user from permission + * @param int $user_id User id to deny + * @param int $perm_par Permission id or name to deny + * @return bool Deny success/failure + */ + public function deny_user($user_id, $perm_par) { + + $perm_id = $this->get_perm_id($perm_par); + + $this->CI->db->where('user_id', $user_id); + $this->CI->db->where('perm_id', $perm_id); + + return $this->CI->db->delete($this->config_vars['perm_to_user']); } + //tested /** - * Allow + * Allow Group * Add group to permission * @param int|string|bool $group_par Group id or name to allow * @param int $perm_par Permission id or name to allow * @return bool Allow success/failure */ - public function allow($group_par, $perm_par) { + public function allow_group($group_par, $perm_par) { $perm_id = $this->get_perm_id($perm_par); + $group_id = $this->get_group_id($group_par); - $query = $this->CI->db->where('group_id',$group_par); + $query = $this->CI->db->where('group_id',$group_id); $query = $this->CI->db->where('perm_id',$perm_id); $query = $this->CI->db->get($this->config_vars['perm_to_group']); if ($query->num_rows() < 1) { - $group_par = $this->get_group_id($group_par); $data = array( - 'group_id' => $group_par, + 'group_id' => $group_id, 'perm_id' => $perm_id ); return $this->CI->db->insert($this->config_vars['perm_to_group'], $data); } + return true; } + //tested /** - * Deny + * Deny Group * Remove group from permission * @param int|string|bool $group_par Group id or name to deny * @param int $perm_par Permission id or name to deny * @return bool Deny success/failure */ - public function deny($group_par, $perm_par) { + public function deny_group($group_par, $perm_par) { $perm_id = $this->get_perm_id($perm_par); + $group_id = $this->get_group_id($group_par); - $group_par = $this->get_group_id($group_par); - $this->CI->db->where('group_id', $group_par); + $this->CI->db->where('group_id', $group_id); $this->CI->db->where('perm_id', $perm_id); return $this->CI->db->delete($this->config_vars['perm_to_group']); } + //tested /** * List Permissions * List all permissions @@ -1158,6 +1357,7 @@ class Aauth { return $query->result(); } + //tested /** * Get permission id * Get permission id from permisison name or id @@ -1182,6 +1382,7 @@ class Aauth { # Private Message Functions ######################## + //tested /** * Send Private Message * Send a private message to another user @@ -1209,6 +1410,18 @@ class Aauth { return false; } + $query = $this->CI->db->where('id', $sender_id); + $query = $this->CI->db->where('banned', 0); + + $query = $this->CI->db->get( $this->config_vars['users'] ); + + // if user not exist or banned + if ( $query->num_rows() < 1 ){ + $this->error($this->config_vars['no_user']); + return false; + } + + $data = array( 'sender_id' => $sender_id, 'receiver_id' => $receiver_id, @@ -1220,6 +1433,7 @@ class Aauth { return $query = $this->CI->db->insert( $this->config_vars['pms'], $data ); } + //tested /** * List Private Messages * If receiver id not given retruns current user's pms, if sender_id given, it returns only pms from given sender @@ -1246,6 +1460,7 @@ class Aauth { return $query->result(); } + //tested /** * Get Private Message * Get private message by id @@ -1255,8 +1470,6 @@ class Aauth { */ public function get_pm($pm_id, $set_as_read = true){ - if ($set_as_read) $this->set_as_read_pm($pm_id); - $query = $this->CI->db->where('id', $pm_id); $query = $this->CI->db->get( $this->config_vars['pms'] ); @@ -1264,9 +1477,12 @@ class Aauth { $this->error( $this->config_vars['no_pm'] ); } + if ($set_as_read) $this->set_as_read_pm($pm_id); + return $query->result(); } + //tested /** * Delete Private Message * Delete private message by id @@ -1278,6 +1494,7 @@ class Aauth { return $this->CI->db->delete( $this->config_vars['pms'], array('id' => $pm_id) ); } + //tested /** * Count unread Private Message * Count number of unread private messages @@ -1290,13 +1507,14 @@ class Aauth { $receiver_id = $this->CI->session->userdata('id'); } - $query = $this->CI->db->where('reciever_id', $receiver_id); + $query = $this->CI->db->where('receiver_id', $receiver_id); $query = $this->CI->db->where('read', 0); $query = $this->CI->db->get( $this->config_vars['pms'] ); return $query->num_rows(); } + //tested /** * Set Private Message as read * Set private message as read @@ -1312,9 +1530,10 @@ class Aauth { } ######################## - # Error/Info Functions + # Error / Info Functions ######################## + //tested /** * Error * Add message to error array and set flash data @@ -1326,6 +1545,19 @@ class Aauth { $this->CI->session->set_flashdata('errors', $this->errors); } + //not working + /** + * Keep Errors + * keeps the flash data flash data + * Benefitial by using Ajax Requests + * more info about flash data + * http://ellislab.com/codeigniter/user-guide/libraries/sessions.html + */ + public function keep_errors(){ + $this->CI->session->keep_flashdata('errors'); + } + + //tested /** * Get Errors Array * Return array of errors @@ -1340,13 +1572,13 @@ class Aauth { } } + //tested /** - * Get Errors - * Return string of errors separated by delimiter + * Print Errors + * Prints string of errors separated by delimiter * @param string $divider Separator for errors - * @return string String of errors separated by delimiter */ - public function get_errors($divider = '
'){ + public function print_errors($divider = '
'){ $msg = ''; $msg_num = count($this->errors); @@ -1359,9 +1591,10 @@ class Aauth { $i++; } - return $msg; + echo $msg; } + //tested /** * Info * Add message to info array and set flash data @@ -1373,6 +1606,19 @@ class Aauth { $this->CI->session->set_flashdata('infos', $this->errors); } + //not working + /** + * Keep Infos + * keeps the flash data + * Benefitial by using Ajax Requests + * more info about flash data + * http://ellislab.com/codeigniter/user-guide/libraries/sessions.html + */ + public function keep_infos(){ + $this->session->keep_flashdata('infos'); + } + + //tested /** * Get Info Array * Return array of info @@ -1387,13 +1633,14 @@ class Aauth { } } + //tested /** - * Get Info - * Return string of info separated by delimiter + * Print Info + * Print string of info separated by delimiter * @param string $divider Separator for info - * @return string String of info separated by delimiter + * */ - public function get_infos($divider = '
'){ + public function print_infos($divider = '
'){ $msg = ''; $msg_num = count($this->infos); @@ -1406,29 +1653,262 @@ class Aauth { $i++; } - return $msg; + echo $msg; } -} + ######################## + # User Variables + ######################## + + //tested + /** + * Set User Variable as key value + * if variable not set before, it will ve set + * if set, overwrites the value + * @param string $key + * @param string $value + * @param int $user_id ; if not given current user + * @return bool + */ + public function set_user_var( $key, $value, $user_id = false ) { + + if ( ! $user_id ){ + $user_id = $this->CI->session->userdata('id'); + } + + // if specified user is not found + if ( ! $this->get_user($user_id)){ + return false; + } + + // if var not set, set + if ( ! $this->get_user_var($key,$user_id) ) { + + $data = array( + 'key' => $key, + 'value' => $value, + 'user_id' => $user_id + ); + + return $this->CI->db->insert( $this->config_vars['user_variables'] , $data); + } + // if var already set, overwrite + else { + + $data = array( + 'key' => $key, + 'value' => $value, + 'user_id' => $user_id + ); + + $this->CI->db->where( 'key', $key ); + $this->CI->db->where( 'user_id', $user_id); + + return $this->CI->db->update( $this->config_vars['user_variables'], $data); + } + } + + //tested + /** + * Unset User Variable as key value + * @param string $key + * @param int $user_id ; if not given current user + * @return bool + */ + public function unset_user_var( $key, $user_id = false ) { + + if ( ! $user_id ){ + $user_id = $this->CI->session->userdata('id'); + } + + // if specified user is not found + if ( ! $this->get_user($user_id)){ + return false; + } + + $this->CI->db->where('key', $key); + $this->CI->db->where('user_id', $user_id); + + return $this->CI->db->delete( $this->config_vars['user_variables'] ); + } + + //tested + /** + * Get User Variable by key + * Return string of variable value or false + * @param string $key + * @param int $user_id ; if not given current user + * @return bool|string , false if var is not set, the value of var if set + */ + public function get_user_var( $key, $user_id = false){ + + if ( ! $user_id ){ + $user_id = $this->CI->session->userdata('id'); + } + + // if specified user is not found + if ( ! $this->get_user($user_id)){ + return false; + } + + $query = $this->CI->db->where('user_id', $user_id); + $query = $this->CI->db->where('key', $key); + + $query = $this->CI->db->get( $this->config_vars['user_variables'] ); + + // if variable not set + if ($query->num_rows() < 1) { return false;} + + else { + + $row = $query->row(); + return $row->value; + } + + } + + ######################## + # Aauth System Variables + ######################## + //tested + /** + * Set Aauth System Variable as key value + * if variable not set before, it will be set + * if set, overwrites the value + * @param string $key + * @param string $value + * @return bool + */ + public function set_system_var( $key, $value ) { + + // if var not set, set + if ( ! $this->get_system_var($key) ) { + + $data = array( + 'key' => $key, + 'value' => $value, + ); + + return $this->CI->db->insert( $this->config_vars['system_variables'] , $data); + + } + // if var already set, overwrite + else { + + $data = array( + 'key' => $key, + 'value' => $value, + ); + + $this->CI->db->where( 'key', $key ); + return $this->CI->db->update( $this->config_vars['system_variables'], $data); + } + + } + + //tested + /** + * Unset Aauth System Variable as key value + * @param string $key + * @return bool + */ + public function unset_system_var( $key ) { + + $this->CI->db->where('key', $key); + + return $this->CI->db->delete( $this->config_vars['system_variables'] ); + } + + //tested + /** + * Get Aauth System Variable by key + * Return string of variable value or false + * @param string $key + * @return bool|string , false if var is not set, the value of var if set + */ + public function get_system_var( $key ){ + + $query = $this->CI->db->where('key', $key); + + $query = $this->CI->db->get( $this->config_vars['system_variables'] ); + + // if variable not set + if ($query->num_rows() < 1) { return false;} + + else { + + $row = $query->row(); + return $row->value; + } + } + +} // end class + +// $this->CI->session->userdata('id') + +/* coming with v3 +---------------- + * captcha (hmm bi bakalım) + * parametre olarak array alma + * stacoverflow + * public id sini 0 a eşitleyip öyle kontrol yapabilirdik (oni boşver uşağum) + * lock_user (until parametrsi) + * unlock_user + * send_pm() in errounda receiver ve sender için ayrı errorlar olabilür + * ddos protect olayını daha mantıklı hale getür + * geçici ban ve e-mail ile tkrar aktifleştime olayı +*/ /** * Coming with v2 * ------------- - * public id sini 0 a eşitleyip öyle kontrol yapabilirdik - * permission id yi permission parametre yap - * performance impr. // tablo isimlerini configden çekmesin - * captcha + * + * tmam // permission id yi permission parametre yap * mail fonksiyonları imtihanı - * config - * stacoverflow - * login e ip aderesi de eklemek lazım - * list_users da grup_par verilirse ve adamın birden fazla grubu varsa nolurkun? - * eğer grup silinmişse kullanıcıları da o gruptan sil (fire) - * ismember la is admine 2. parametre olarak user id ekle - * kepp infos errors die bişey yap ajax requestlerinde silinir errorlar + * tamam // login e ip aderesi de eklemek lazım + * list_users da grup_par verilirse ve adamın birden fazla grubu varsa nolurkun? // bi denemek lazım belki distinct ile düzelir + * tamam // eğer grup silinmişse kullanıcıları da o gruptan sil (fire) + * tamam // ismember la is admine 2. parametre olarak user id ekle + * tamam // kepp infos errors die bişey yap ajax requestlerinde silinir errorlar + * tmam // user variables + * tamam // sistem variables + * tmam // user perms + * tamam gibi // 4mysql index fulltext index?? + * tamam //delete_user dan sonra grup ve perms ler de silinmeli + * login() içinde login'i doğru şekilde olsa da yine de login attempt artıyo kesin düzeltilecek + * keep_errors ve keep_infos calismiyor + * + * + * + * ----------- + * ok + * + * unban_user() added // unlock_user + * remove member added // fire_member + * allow() changed to allow_group + * deny() changed to deny_group + * is member a yeni parametre eklendi + * allow_user() added + * deny_user() added + * keep_infos() added + * kepp_errors() added + * get_errors() changed to print_errors() + * get_infos() changed to print_infos() + * User and Aauth System Variables. +set_user_var( $key, $value, $user_id = false ) +get_user_var( $key, $user_id = false) +unset +set_system_var( $key, $value, $user_id = false ) +get_system_var( $key, $user_id = false) +unset +functions added * - * Done staff + * + * + * + * + * Done staff v1 * ----------- * tamam hacı // control die bi fonksiyon yazıp adam önce login omuşmu sonra da yetkisi var mı die kontrol et. yetkisi yoksa yönlendir ve aktivitiyi güncelle * tamam hacı // grupları yetkilendirme, yetki ekleme, alma alow deny @@ -1448,4 +1928,25 @@ class Aauth { * tamam ama engelleme ve limit olayı koymadım. // pm için okundu ve göster, sil, engelle? die fonksiyonlar eklencek , gönderilen pmler, alınan pmler, arasındaki pmler, * tamm// already existedleri info yap onlar error değil hacım * + + + + +/* +// if user's email is found +if ($query->num_rows() > 0) { +$row = $query->row(); + +// DDos protection +if ( $this->config_vars['dos_protection'] and $row->last_login_attempt != '' and +(strtotime("now") + 30 * $this->config_vars['try'] ) < strtotime($row->last_login_attempt) ) { +$this->error($this->config_vars['exceeded']); +return false; +} +} */ + + + + + diff --git a/sql/Aauth_v2.sql b/sql/Aauth_v2.sql new file mode 100644 index 0000000..52aafbd --- /dev/null +++ b/sql/Aauth_v2.sql @@ -0,0 +1,171 @@ +/* +Navicat MySQL Data Transfer + +Source Server : local +Source Server Version : 50508 +Source Host : localhost:3306 +Source Database : aauth_v2_dev + +Target Server Type : MYSQL +Target Server Version : 50508 +File Encoding : 65001 + +Date: 2014-07-03 21:23:21 +*/ + +SET FOREIGN_KEY_CHECKS=0; + +-- ---------------------------- +-- Table structure for `aauth_groups` +-- ---------------------------- +DROP TABLE IF EXISTS `aauth_groups`; +CREATE TABLE `aauth_groups` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` text, + PRIMARY KEY (`id`), + KEY `id_index` (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of aauth_groups +-- ---------------------------- +INSERT INTO `aauth_groups` VALUES ('1', 'Admin'); +INSERT INTO `aauth_groups` VALUES ('2', 'Public'); +INSERT INTO `aauth_groups` VALUES ('3', 'Default'); + +-- ---------------------------- +-- Table structure for `aauth_perms` +-- ---------------------------- +DROP TABLE IF EXISTS `aauth_perms`; +CREATE TABLE `aauth_perms` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` text, + `definition` text, + PRIMARY KEY (`id`), + KEY `id_index` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of aauth_perms +-- ---------------------------- + +-- ---------------------------- +-- Table structure for `aauth_perm_to_group` +-- ---------------------------- +DROP TABLE IF EXISTS `aauth_perm_to_group`; +CREATE TABLE `aauth_perm_to_group` ( + `perm_id` int(11) DEFAULT NULL, + `group_id` int(11) DEFAULT NULL, + KEY `perm_id_group_id_index` (`perm_id`,`group_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of aauth_perm_to_group +-- ---------------------------- + +-- ---------------------------- +-- Table structure for `aauth_perm_to_user` +-- ---------------------------- +DROP TABLE IF EXISTS `aauth_perm_to_user`; +CREATE TABLE `aauth_perm_to_user` ( + `perm_id` int(11) DEFAULT NULL, + `user_id` int(11) DEFAULT NULL, + KEY `perm_id_user_id_index` (`perm_id`,`user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of aauth_perm_to_user +-- ---------------------------- + +-- ---------------------------- +-- Table structure for `aauth_pms` +-- ---------------------------- +DROP TABLE IF EXISTS `aauth_pms`; +CREATE TABLE `aauth_pms` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `sender_id` int(11) NOT NULL, + `receiver_id` int(11) NOT NULL, + `title` text NOT NULL, + `message` text, + `date` datetime DEFAULT NULL, + `read` int(11) DEFAULT '0', + PRIMARY KEY (`id`), + KEY `full_index` (`id`,`sender_id`,`receiver_id`,`read`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of aauth_pms +-- ---------------------------- + +-- ---------------------------- +-- Table structure for `aauth_system_variables` +-- ---------------------------- +DROP TABLE IF EXISTS `aauth_system_variables`; +CREATE TABLE `aauth_system_variables` ( + `key` text NOT NULL, + `value` text +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of aauth_system_variables +-- ---------------------------- + +-- ---------------------------- +-- Table structure for `aauth_users` +-- ---------------------------- +DROP TABLE IF EXISTS `aauth_users`; +CREATE TABLE `aauth_users` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `email` text COLLATE utf8_turkish_ci NOT NULL, + `pass` text COLLATE utf8_turkish_ci NOT NULL, + `name` text COLLATE utf8_turkish_ci, + `banned` int(11) DEFAULT '0', + `last_login` datetime DEFAULT NULL, + `last_activity` datetime DEFAULT NULL, + `last_login_attempt` datetime DEFAULT NULL, + `forgot_exp` text COLLATE utf8_turkish_ci, + `remember_time` datetime DEFAULT NULL, + `remember_exp` text COLLATE utf8_turkish_ci, + `verification_code` text COLLATE utf8_turkish_ci, + `ip_address` text COLLATE utf8_turkish_ci, + `login_attempts` int(11) DEFAULT '0', + PRIMARY KEY (`id`), + KEY `id_index` (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci; + +-- ---------------------------- +-- Records of aauth_users +-- ---------------------------- +INSERT INTO `aauth_users` VALUES ('1', 'admin@admin.com', 'dd5073c93fb477a167fd69072e95455834acd93df8fed41a2c468c45b394bfe3', 'Admin', '0', null, null, null, null, null, null, null, null, '0'); + +-- ---------------------------- +-- Table structure for `aauth_user_to_group` +-- ---------------------------- +DROP TABLE IF EXISTS `aauth_user_to_group`; +CREATE TABLE `aauth_user_to_group` ( + `user_id` int(11) NOT NULL DEFAULT '0', + `group_id` int(11) NOT NULL DEFAULT '0', + PRIMARY KEY (`user_id`,`group_id`), + KEY `user_id_group_id_index` (`user_id`,`group_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of aauth_user_to_group +-- ---------------------------- +INSERT INTO `aauth_user_to_group` VALUES ('1', '1'); +INSERT INTO `aauth_user_to_group` VALUES ('1', '3'); + +-- ---------------------------- +-- Table structure for `aauth_user_variables` +-- ---------------------------- +DROP TABLE IF EXISTS `aauth_user_variables`; +CREATE TABLE `aauth_user_variables` ( + `user_id` int(11) NOT NULL, + `key` text NOT NULL, + `value` text, + KEY `user_id_index` (`user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of aauth_user_variables +-- ---------------------------- diff --git a/sql/aauth.sql b/sql/aauth.sql deleted file mode 100644 index 93704cd..0000000 --- a/sql/aauth.sql +++ /dev/null @@ -1,144 +0,0 @@ --- phpMyAdmin SQL Dump --- version 3.3.9 --- http://www.phpmyadmin.net --- --- Anamakine: localhost --- Üretim Zamanı: 18 Eylül 2013 saat 10:18:09 --- Sunucu sürümü: 5.5.8 --- PHP Sürümü: 5.3.5 - -SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; - - -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; -/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; -/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; -/*!40101 SET NAMES utf8 */; - --- --- Veritabanı: `aauth2` --- - --- -------------------------------------------------------- - --- --- Tablo için tablo yapısı `aauth_groups` --- - -CREATE TABLE IF NOT EXISTS `aauth_groups` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` text, - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ; - --- --- Tablo döküm verisi `aauth_groups` --- - -INSERT INTO `aauth_groups` (`id`, `name`) VALUES -(1, 'admin'), -(2, 'public'), -(3, 'default'); - --- -------------------------------------------------------- - --- --- Tablo için tablo yapısı `aauth_perms` --- - -CREATE TABLE IF NOT EXISTS `aauth_perms` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` text, - `definition` text, - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; - --- --- Tablo döküm verisi `aauth_perms` --- - - --- -------------------------------------------------------- - --- --- Tablo için tablo yapısı `aauth_perm_to_group` --- - -CREATE TABLE IF NOT EXISTS `aauth_perm_to_group` ( - `perm_id` int(11) DEFAULT NULL, - `group_id` int(11) DEFAULT NULL -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - --- --- Tablo döküm verisi `aauth_perm_to_group` --- - - --- -------------------------------------------------------- - --- --- Tablo için tablo yapısı `aauth_pm` --- - -CREATE TABLE IF NOT EXISTS `aauth_pm` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `sender_id` int(11) NOT NULL, - `receiver_id` int(11) NOT NULL, - `message` text, - `date` datetime DEFAULT NULL, - `read` int(11) DEFAULT '0', - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; - --- --- Tablo döküm verisi `aauth_pm` --- - - --- -------------------------------------------------------- - --- --- Tablo için tablo yapısı `aauth_users` --- - -CREATE TABLE IF NOT EXISTS `aauth_users` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `email` text COLLATE utf8_turkish_ci NOT NULL, - `pass` text COLLATE utf8_turkish_ci NOT NULL, - `name` text COLLATE utf8_turkish_ci, - `banned` int(11) DEFAULT '0', - `last_login` datetime DEFAULT NULL, - `last_activity` datetime DEFAULT NULL, - `last_login_attempt` datetime DEFAULT NULL, - `forgot_exp` text COLLATE utf8_turkish_ci, - `remember_time` datetime DEFAULT NULL, - `remember_exp` text COLLATE utf8_turkish_ci, - `verification_code` text COLLATE utf8_turkish_ci, - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=2 ; - --- --- Tablo döküm verisi `aauth_users` --- - -INSERT INTO `aauth_users` (`id`, `email`, `pass`, `name`, `banned`, `last_login`, `last_activity`, `last_login_attempt`, `forgot_exp`, `remember_time`, `remember_exp`, `verification_code`) VALUES -(1, 'admin@admin.com', 'admin pass', 'Admin', 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL); - --- -------------------------------------------------------- - --- --- Tablo için tablo yapısı `aauth_user_to_group` --- - -CREATE TABLE IF NOT EXISTS `aauth_user_to_group` ( - `user_id` int(11) NOT NULL DEFAULT '0', - `group_id` int(11) NOT NULL DEFAULT '0', - PRIMARY KEY (`user_id`,`group_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - --- --- Tablo döküm verisi `aauth_user_to_group` --- - -INSERT INTO `aauth_user_to_group` (`user_id`, `group_id`) VALUES -(1, 1);