PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/application/models/client_model.php

https://github.com/iRail/PlugID
PHP | 101 lines | 55 code | 15 blank | 31 comment | 5 complexity | f14738827d28c8089bb54bd2f1b197bb MD5 | raw file
  1. <?php
  2. /**
  3. * @copyright (C) 2012 by iRail vzw/asbl
  4. * @license AGPLv3
  5. * @author Jens Segers <jens at iRail.be>
  6. * @author Hannes Van De Vreken <hannes at iRail.be>
  7. */
  8. class Client_model extends CI_Model {
  9. private $hash_algo = 'md5';
  10. function get($client_id) {
  11. // obvs.
  12. $where = array('client_id' => $client_id);
  13. return $this->db->get_where('clients', $where)->row();
  14. }
  15. /*
  16. *
  17. */
  18. function validate_secret( $client_id, $client_secret ){
  19. $where = array( 'client_id' => $client_id,
  20. 'client_secret' => $client_secret);
  21. return $this->db->get_where('clients', $where)->num_rows() == 1 ;
  22. }
  23. /*
  24. *
  25. */
  26. function validate_redirect_uri( $client_id, $redirect_uri ){
  27. $where = array( 'client_id' => $client_id,
  28. 'redirect_uri' => $redirect_uri );
  29. return $this->db->get_where('clients', $where)->num_rows() == 1 ;
  30. }
  31. /*
  32. * Generate new client_id & client_secret
  33. */
  34. function create( $name, $redirect_uri, $user_id ){
  35. // generate data
  36. $data = new stdClass();
  37. $data->client_id = hash($this->hash_algo, time() . uniqid());
  38. $data->client_secret = hash($this->hash_algo, time() . uniqid());
  39. $data->name = $name ;
  40. $data->redirect_uri = $redirect_uri;
  41. $data->user_id = $user_id;
  42. // check if client_exists
  43. while( $this->db->get_where('clients', array('client_id' => $data->client_id ) )->num_rows() == 1 ){
  44. $data->client_id = md5(time() . uniqid());
  45. }
  46. // insert row
  47. $this->db->insert('clients',(array)$data);
  48. return $data ;
  49. }
  50. /*
  51. * Reset client's secret
  52. */
  53. function reset_secret($client_id, $type)
  54. {
  55. // create new secret
  56. $data = array(
  57. $type => hash($this->hash_algo, time() . uniqid()),
  58. );
  59. // update db
  60. $this->db->update( 'clients', $data, array('client_id' => $client_id ) );
  61. // return result
  62. $data->client_id = $client_id;
  63. return $data ;
  64. }
  65. /*
  66. * set some data
  67. */
  68. function update( $client_id, $name, $redirect_uri, $notify_uri = NULL){
  69. if (is_null($name) || is_null($redirect_uri)) return false;
  70. $data = array(
  71. 'name' => $name,
  72. 'redirect_uri' => $redirect_uri,
  73. 'notify_uri' => $notify_uri,
  74. );
  75. return $this->db->update( 'clients', $data, array('client_id' => $client_id ));
  76. }
  77. /*
  78. * remove completely
  79. */
  80. function delete( $client_id ){
  81. $where = array('client_id' => $client_id);
  82. $tables = array('clients','auth_codes','auth_tokens');
  83. $this->db->delete($tables, $where);
  84. return true ;
  85. }
  86. }