| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | namespace SimpleID\Models; |
| 24: | |
| 25: | use \Base; |
| 26: | use \Serializable; |
| 27: | use SimpleID\ModuleManager; |
| 28: | use SimpleID\Crypt\OpaqueIdentifier; |
| 29: | use SimpleID\Store\Storable; |
| 30: | use SimpleID\Util\ArrayWrapper; |
| 31: | use SimpleID\Util\Events\BaseDataCollectionEvent; |
| 32: | use SimpleID\Base\UserModule; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | class User extends ArrayWrapper implements Serializable, Storable { |
| 38: | |
| 39: | const ACTIVITY_LOG_SIZE = 10; |
| 40: | |
| 41: | |
| 42: | protected $uid; |
| 43: | |
| 44: | |
| 45: | protected $activities = []; |
| 46: | |
| 47: | |
| 48: | public $clients = []; |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | public function __construct($data = [ 'openid' => [] ]) { |
| 54: | parent::__construct($data); |
| 55: | } |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | public function isAdministrator() { |
| 63: | return ($this->container['administrator']); |
| 64: | } |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | public function hasLocalOpenIDIdentity() { |
| 72: | return isset($this->container['openid']['identity']); |
| 73: | } |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | public function getLocalOpenIDIdentity() { |
| 81: | return ($this->hasLocalOpenIDIdentity()) ? $this->container['openid']['identity'] : null; |
| 82: | } |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | public function getPairwiseIdentity($sector_identifier) { |
| 97: | $opaque = new OpaqueIdentifier(); |
| 98: | return 'pwid:' . $opaque->generate($sector_identifier . ':' . $this->uid); |
| 99: | } |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | public function getDisplayName() { |
| 108: | if (isset($this->container['userinfo']['name'])) return $this->container['userinfo']['name']; |
| 109: | if (isset($this->container['userinfo']['given_name']) && isset($this->container['userinfo']['family_name'])) |
| 110: | return $this->container['userinfo']['given_name'] . ' ' . $this->container['userinfo']['family_name']; |
| 111: | return $this->uid; |
| 112: | } |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | public function addActivity($id, $data) { |
| 129: | $this->activities[$id] = $data; |
| 130: | uasort($this->activities, function($a, $b) { |
| 131: | if ($a['time'] == $b['time']) { return 0; } return ($a['time'] < $b['time']) ? 1 : -1; |
| 132: | }); |
| 133: | if (count($this->activities) > self::ACTIVITY_LOG_SIZE) { |
| 134: | $this->activities = array_slice($this->activities, 0, self::ACTIVITY_LOG_SIZE); |
| 135: | } |
| 136: | } |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | public function getActivities() { |
| 144: | return $this->activities; |
| 145: | } |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | public function loadData($data) { |
| 155: | parent::loadData($data); |
| 156: | if ($data instanceof User) { |
| 157: | $this->activities = $data->activities; |
| 158: | $this->clients = $data->clients; |
| 159: | } |
| 160: | } |
| 161: | |
| 162: | public function offsetSet($offset, $value): void { |
| 163: | switch ($offset) { |
| 164: | case 'uid': |
| 165: | $this->uid = $value; |
| 166: | break; |
| 167: | case 'display_name': |
| 168: | return; |
| 169: | case 'identity': |
| 170: | $this->container['openid']['identity'] = $value; |
| 171: | break; |
| 172: | default: |
| 173: | parent::offsetSet($offset, $value); |
| 174: | } |
| 175: | } |
| 176: | |
| 177: | public function offsetExists($offset): bool { |
| 178: | switch ($offset) { |
| 179: | case 'uid': |
| 180: | case 'display_name': |
| 181: | return true; |
| 182: | case 'identity': |
| 183: | return $this->hasLocalOpenIDIdentity(); |
| 184: | default: |
| 185: | return parent::offsetExists($offset); |
| 186: | } |
| 187: | } |
| 188: | |
| 189: | public function offsetGet($offset): mixed { |
| 190: | switch ($offset) { |
| 191: | case 'uid': |
| 192: | return $this->uid; |
| 193: | case 'display_name': |
| 194: | return $this->getDisplayName(); |
| 195: | case 'identity': |
| 196: | |
| 197: | $mod = UserModule::instance(); |
| 198: | return ($this->hasLocalOpenIDIdentity()) ? $this->getLocalOpenIDIdentity() : $mod->getCanonicalURL('user/' . rawurlencode($this['uid'])); |
| 199: | default: |
| 200: | return parent::offsetGet($offset); |
| 201: | } |
| 202: | } |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | private function toSecureArray($hidden_value = null) { |
| 209: | $event = new BaseDataCollectionEvent('user_secret_data_paths'); |
| 210: | $copy = new ArrayWrapper($this->container); |
| 211: | \Events::instance()->dispatch($event); |
| 212: | $secret_paths = $event->getResults(); |
| 213: | if ($secret_paths == null) $secret_paths = []; |
| 214: | $secret_paths[] = 'uid'; |
| 215: | foreach ($secret_paths as $path) { |
| 216: | if ($hidden_value) { |
| 217: | $copy->set($path, $hidden_value); |
| 218: | } else { |
| 219: | $copy->unset($path); |
| 220: | } |
| 221: | } |
| 222: | return $copy->toArray(); |
| 223: | } |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | public function serialize() { |
| 229: | $f3 = Base::instance(); |
| 230: | return $f3->serialize($this->__serialize()); |
| 231: | } |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | public function __serialize() { |
| 240: | $result = []; |
| 241: | foreach (get_object_vars($this) as $var => $value) { |
| 242: | if ($var == 'container') { |
| 243: | $result['container'] = $this->toSecureArray(); |
| 244: | } else { |
| 245: | $result[$var] = $value; |
| 246: | } |
| 247: | } |
| 248: | return $result; |
| 249: | } |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | public function unserialize($data) { |
| 255: | $f3 = Base::instance(); |
| 256: | |
| 257: | |
| 258: | $array = $f3->unserialize($data); |
| 259: | $this->__unserialize($array); |
| 260: | } |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | public function __unserialize($array) { |
| 270: | foreach ($array as $var => $value) { |
| 271: | $this->$var = $value; |
| 272: | } |
| 273: | } |
| 274: | |
| 275: | public function getStoreType() { |
| 276: | return 'user'; |
| 277: | } |
| 278: | |
| 279: | |
| 280: | public function getStoreID() { |
| 281: | return $this->uid; |
| 282: | } |
| 283: | |
| 284: | |
| 285: | public function setStoreID($id) { |
| 286: | $this->uid = $id; |
| 287: | } |
| 288: | |
| 289: | |
| 290: | |
| 291: | |
| 292: | |
| 293: | |
| 294: | |
| 295: | public function toString() { |
| 296: | $result = []; |
| 297: | foreach (get_object_vars($this) as $var => $value) { |
| 298: | if ($var == 'container') { |
| 299: | $result['container'] = $this->toSecureArray('[hidden]'); |
| 300: | } else { |
| 301: | $result[$var] = $value; |
| 302: | } |
| 303: | } |
| 304: | |
| 305: | return print_r($result, true); |
| 306: | } |
| 307: | } |
| 308: | |
| 309: | ?> |
| 310: | |