├── 001_Create_ion_auth.php └── README /001_Create_ion_auth.php: -------------------------------------------------------------------------------- 1 | use_config) { 23 | $this->config->load('ion_auth', TRUE); 24 | $tables = $this->config->item('tables', 'ion_auth'); 25 | $joins = $this->config->item('join', 'ion_auth'); 26 | 27 | // table names 28 | $this->groups = $tables['groups']; 29 | $this->users = $tables['users']; 30 | $this->login_attempts = $tables['login_attempts']; 31 | // join names 32 | $this->groups_join = $joins['groups']; 33 | $this->users_join = $joins['users']; 34 | } 35 | 36 | } 37 | 38 | function up() 39 | { 40 | /* 41 | * In order to add default data with migrations 42 | */ 43 | //$this->load->library('database'); 44 | 45 | // Function to use config variables 46 | $this->use_config(); 47 | 48 | // groups 49 | if (!$this->db->table_exists($this->groups)) 50 | { 51 | // Setup Keys 52 | $this->dbforge->add_key('id', TRUE); 53 | 54 | $this->dbforge->add_field(array( 55 | 'id' => array('type' => 'MEDIUMINT', 'constraint' => 8, 'unsigned' => TRUE, 'null' => FALSE, 'auto_increment' => TRUE), 56 | 'name' => array('type' => 'VARCHAR', 'constraint' => '20', 'null' => FALSE), 57 | 'description' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE) 58 | )); 59 | // create table 60 | $this->dbforge->create_table($this->groups, TRUE); 61 | 62 | // default data 63 | $this->db->insert($this->groups, array('id'=>null,'name'=>'admin','description'=>'Administrator')); 64 | $this->db->insert($this->groups, array('id'=>null,'name'=>'members','description'=>'General User')); 65 | } 66 | 67 | // users 68 | if (!$this->db->table_exists($this->users)) 69 | { 70 | // Setup Keys 71 | $this->dbforge->add_key('id', TRUE); 72 | 73 | $this->dbforge->add_field(array( 74 | 'id' => array('type' => 'MEDIUMINT', 'constraint' => 8, 'unsigned' => TRUE, 'null' => FALSE, 'auto_increment' => TRUE), 75 | 'ip_address' => array('type' => 'VARBINARY', 'constraint' => '16', 'null' => FALSE), 76 | 'username' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE), 77 | 'password' => array('type' => 'VARCHAR', 'constraint' => '80', 'null' => FALSE), 78 | 'salt' => array('type' => 'VARCHAR', 'constraint' => '40', 'null' => TRUE), 79 | 'email' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE), 80 | 'activation_code' => array('type' => 'VARCHAR', 'constraint' => '40', 'null' => TRUE), 81 | 'forgotten_password_code' => array('type' => 'VARCHAR', 'constraint' => '40', 'null' => TRUE), 82 | 'forgotten_password_time' => array('type' => 'int', 'constraint' => '11', 'unsigned' => TRUE, 'null' => TRUE), 83 | 'remember_code' => array('type' => 'VARCHAR', 'constraint' => '40', 'null' => TRUE), 84 | 'created_on' => array('type' => 'int', 'constraint' => '11', 'unsigned' => TRUE, 'null' => FALSE), 85 | 'last_login' => array('type' => 'int', 'constraint' => '11', 'unsigned' => TRUE, 'null' => TRUE), 86 | 'active' => array('type' => 'tinyint', 'constraint' => '1', 'unsigned' => TRUE, 'null' => TRUE), 87 | 'first_name' => array('type' => 'VARCHAR', 'constraint' => '50', 'null' => TRUE), 88 | 'last_name' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => TRUE), 89 | 'company' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => TRUE), 90 | 'phone' => array('type' => 'VARCHAR', 'constraint' => '50', 'null' => TRUE) 91 | )); 92 | // create table 93 | $this->dbforge->create_table($this->users, TRUE); 94 | 95 | // default data 96 | $data = array( 97 | 'ip_address'=> inet_pton('127.0.0.1'), 98 | 'username'=>'administrator', 99 | 'password'=>'59beecdf7fc966e2f17fd8f65a4a9aeb09d4a3d4', 100 | 'salt'=>'9462e8eee0', 101 | 'email'=>'admin@admin.com', 102 | 'activation_code'=>'', 103 | 'forgotten_password_code'=>NULL, 104 | 'forgotten_password_time'=>NULL, 105 | 'created_on'=>'1268889823', 106 | 'last_login'=>'1268889823', 107 | 'active'=>'1', 108 | 'first_name' => 'Admin', 109 | 'last_name' => 'istrator', 110 | 'company' => 'ADMIN', 111 | 'phone' => '0' 112 | ); 113 | $this->db->insert($this->users, $data); 114 | } 115 | 116 | // users_groups 117 | if (!$this->db->table_exists("{$this->users}_{$this->groups}")) 118 | { 119 | // Setup keys 120 | $this->dbforge->add_key('id', TRUE); 121 | 122 | // Build Schema 123 | $this->dbforge->add_field(array( 124 | 'id' => array('type' => 'MEDIUMINT', 'constraint' => 8, 'unsigned' => TRUE, 'null' => FALSE, 'auto_increment' => TRUE), 125 | "$this->users_join" => array('type' => 'MEDIUMINT', 'constraint' => 8, 'unsigned' => TRUE, 'null' => FALSE), 126 | "$this->groups_join" => array('type' => 'MEDIUMINT', 'constraint' => 8, 'unsigned' => TRUE, 'null' => FALSE) 127 | )); 128 | // create table 129 | $this->dbforge->create_table("{$this->users}_{$this->groups}", TRUE); 130 | 131 | // define default data 132 | $data = array( 133 | array( 134 | "$this->users_join" => 1, 135 | "$this->groups_join" => 1 136 | ), 137 | array( 138 | "$this->users_join" => 1, 139 | "$this->groups_join" => 2 140 | ) 141 | ); 142 | // Insert data 143 | $this->db->insert_batch("{$this->users}_{$this->groups}", $data); 144 | } 145 | if (!$this->db->table_exists($this->login_attempts)) 146 | { 147 | // Setup Keys 148 | $this->dbforge->add_key('id', TRUE); 149 | $this->dbforge->add_key('ip_address'); 150 | $this->dbforge->add_key('login'); 151 | 152 | $this->dbforge->add_field(array( 153 | 'id' => array('type' => 'MEDIUMINT', 'constraint' => 8, 'unsigned' => TRUE, 'null' => FALSE, 'auto_increment' => TRUE), 154 | 'ip_address' => array('type' => 'VARBINARY', 'constraint' => '16', 'null' => FALSE), 155 | 'login' => array('type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE), 156 | 'time' => array('type' => 'int', 'constraint' => '11', 'unsigned' => TRUE, 'null' => FALSE) 157 | )); 158 | // create table 159 | $this->dbforge->create_table($this->login_attempts, TRUE); 160 | } 161 | } 162 | 163 | function down() 164 | { 165 | // Function to use config variables if 166 | $this->use_config(); 167 | 168 | $this->dbforge->drop_table($this->groups); 169 | $this->dbforge->drop_table($this->users); 170 | $this->dbforge->drop_table("{$this->users}_{$this->groups}"); 171 | $this->dbforge->drop_table($this->login_attempts); 172 | } 173 | } -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ====================================== 2 | Ion Auth Database Migration 3 | 4 | by Jd Fiscus 5 | ====================================== 6 | 7 | Hey everybody, 8 | 9 | If you are using the ion auth authentication library and database migrations, 10 | this is a default migration and insert data that mimic's ion auth's sql file. 11 | 12 | 13 | INSTALLATION: 14 | - You must have codeigniter-migrations installed (https://github.com/philsturgeon/codeigniter-migrations) 15 | - You must have codeigniter-ion-auth installed (https://github.com/benedmunds/CodeIgniter-Ion-Auth) 16 | 17 | Just copy the files from this package to the corresponding folder in your 18 | application folder. For example, copy 001_Create_ion_auth.php to 19 | application/migrations/001_Create_ion_auth.php. 20 | 21 | DOCUMENTATION: 22 | In the migration file you can define the tables for 'groups', 'meta', and 'users' 23 | to custom table names that you might specify in your ion auth config. This will change all 24 | the instances of the table names correctly. 25 | 26 | If you have the variable $use_config set to TRUE, then the migration will pull in the 27 | correct variables from the Ion Auth config file. 28 | -OR- 29 | You could set them manually if you'd like instead of pulling them in from the config file. 30 | Example: 31 | ```php 32 | // Tables 33 | private $groups = 'access'; 34 | private $meta = 'profile'; 35 | private $users = 'auth'; 36 | ``` 37 | 38 | You can also change the join names to correspond with you custom tables: 39 | ```php 40 | // Join names 41 | private $groups_join = 'access_id'; 42 | private $users_join = 'auth_id'; 43 | ``` 44 | 45 | Thanks, 46 | -Jd Fiscus 47 | jdfiscus@gmail.com 48 | @iamfiscus 49 | --------------------------------------------------------------------------------