1: <?php
2:
3: namespace Alpha\Model;
4:
5: use Alpha\Model\Type\String;
6: use Alpha\Model\Type\Enum;
7: use Alpha\Model\Type\Relation;
8: use Alpha\Util\Helper\Validator;
9: use Alpha\Util\Logging\Logger;
10: use Alpha\Util\Config\ConfigProvider;
11: use Alpha\Util\Email\EmailProviderFactory;
12: use Alpha\Exception\RecordNotFoundException;
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56: class Person extends ActiveRecord
57: {
58: 59: 60: 61: 62: 63: 64:
65: protected $displayName;
66:
67: 68: 69: 70: 71: 72: 73:
74: protected $email;
75:
76: 77: 78: 79: 80: 81: 82:
83: protected $password;
84:
85: 86: 87: 88: 89: 90: 91:
92: protected $rights;
93:
94: 95: 96: 97: 98: 99: 100:
101: protected $actions;
102:
103: 104: 105: 106: 107: 108: 109:
110: protected $dataLabels = array('OID' => 'Member ID#',
111: 'displayName' => 'Display Name',
112: 'email' => 'E-mail Address',
113: 'password' => 'Password',
114: 'state' => 'Account state',
115: 'URL' => 'Your site address',
116: 'rights' => 'Rights Group Membership',
117: 'actions' => 'Actions', );
118:
119: 120: 121: 122: 123: 124: 125:
126: const TABLE_NAME = 'Person';
127:
128: 129: 130: 131: 132: 133: 134:
135: protected $state;
136:
137: 138: 139: 140: 141: 142: 143:
144: protected $URL;
145:
146: 147: 148: 149: 150: 151: 152:
153: private static $logger = null;
154:
155: 156: 157: 158: 159:
160: public function __construct()
161: {
162: self::$logger = new Logger('Person');
163: self::$logger->debug('>>__construct()');
164:
165:
166: parent::__construct();
167: $this->displayName = new String();
168: $this->displayName->setRule(Validator::REQUIRED_USERNAME);
169: $this->displayName->setSize(70);
170: $this->displayName->setHelper('Please provide a name for display on the website (only letters, numbers, and .-_ characters are allowed!).');
171: $this->email = new String();
172: $this->email->setRule(Validator::REQUIRED_EMAIL);
173: $this->email->setSize(70);
174: $this->email->setHelper('Please provide a valid e-mail address as your username.');
175: $this->password = new String();
176: $this->password->setSize(70);
177: $this->password->setHelper('Please provide a password for logging in.');
178: $this->password->isPassword(true);
179: $this->state = new Enum(array(
180: 'Active',
181: 'Disabled', ));
182: $this->state->setValue('Active');
183: $this->URL = new String();
184: $this->URL->setRule(Validator::OPTIONAL_HTTP_URL);
185: $this->URL->setHelper('URLs must be in the format http://some_domain/ or left blank!');
186:
187: $this->markUnique('displayName');
188: $this->markUnique('email');
189:
190: $this->rights = new Relation();
191: $this->markTransient('rights');
192:
193: $this->actions = new Relation();
194: $this->markTransient('actions');
195:
196: $this->setupRels();
197:
198: self::$logger->debug('<<__construct');
199: }
200:
201: 202: 203: 204: 205:
206: protected function after_load_callback()
207: {
208: $this->setupRels();
209: }
210:
211: 212: 213: 214: 215:
216: protected function after_loadByAttribute_callback()
217: {
218: $this->setupRels();
219: }
220:
221: 222: 223: 224: 225: 226: 227:
228: protected function after_save_callback()
229: {
230: if ($this->getVersionNumber()->getValue() == 1) {
231: $standardGroup = new Rights();
232:
233: $this->setupRels();
234:
235: if (!$this->inGroup('Standard')) {
236: try {
237: $standardGroup->loadByAttribute('name', 'Standard');
238: } catch (RecordNotFoundException $e) {
239: $standardGroup->set('name', 'Standard');
240: $standardGroup->save();
241: }
242:
243: $lookup = $this->rights->getLookup();
244: $lookup->setValue(array($this->getID(), $standardGroup->getID()));
245: $lookup->save();
246: }
247: }
248: }
249:
250: 251: 252: 253: 254:
255: protected function before_save_callback()
256: {
257: if (password_needs_rehash($this->get('password'), PASSWORD_DEFAULT, ['cost' => 12])) {
258: $this->set('password', password_hash($this->get('password'), PASSWORD_DEFAULT, ['cost' => 12]));
259: }
260: }
261:
262: 263: 264: 265: 266:
267: protected function setupRels()
268: {
269:
270: if (isset($this->rights)) {
271: $this->rights->setRelatedClass('Alpha\Model\Person', 'left');
272: $this->rights->setRelatedClassDisplayField('email', 'left');
273: $this->rights->setRelatedClass('Alpha\Model\Rights', 'right');
274: $this->rights->setRelatedClassDisplayField('name', 'right');
275: $this->rights->setRelationType('MANY-TO-MANY');
276: $this->rights->setValue($this->getID());
277: }
278:
279: if (isset($this->actions)) {
280: $this->actions->setValue($this->OID);
281: $this->actions->setRelatedClass('Alpha\Model\ActionLog');
282: $this->actions->setRelatedClassField('created_by');
283: $this->actions->setRelatedClassDisplayField('message');
284: $this->actions->setRelationType('ONE-TO-MANY');
285: }
286: }
287:
288: 289: 290: 291: 292: 293: 294:
295: public function setDisplayName($displayName)
296: {
297: $this->displayName->setValue($displayName);
298: }
299:
300: 301: 302: 303: 304: 305: 306:
307: public function getDisplayName()
308: {
309: return $this->displayName;
310: }
311:
312: 313: 314: 315: 316: 317: 318: 319: 320:
321: public function inGroup($groupName)
322: {
323: if (self::$logger == null) {
324: self::$logger = new Logger('Person');
325: }
326: self::$logger->debug('>>inGroup(groupName=['.$groupName.'])');
327:
328: $group = new Rights();
329:
330: try {
331: $group->loadByAttribute('name', $groupName);
332: } catch (RecordNotFoundException $e) {
333: self::$logger->error('Unable to load the group named ['.$groupName.']');
334: self::$logger->debug('<<inGroup [false]');
335:
336: return false;
337: }
338:
339: $rel = $group->getMembers();
340:
341: try {
342:
343: $lookUps = $rel->getLookup()->loadAllByAttribute('leftID', $this->getID());
344: foreach ($lookUps as $lookUp) {
345:
346: $ids = $lookUp->getValue();
347:
348: if ($ids[1] == $group->getID()) {
349: self::$logger->debug('<<inGroup [true]');
350:
351: return true;
352: }
353: }
354: } catch (RecordNotFoundException $e) {
355: self::$logger->debug('<<inGroup [false]');
356:
357: return false;
358: }
359:
360: self::$logger->debug('<<inGroup [false]');
361:
362: return false;
363: }
364:
365: 366: 367: 368: 369: 370: 371: 372: 373:
374: public function addToGroup($groupName)
375: {
376: if (self::$logger == null) {
377: self::$logger = new Logger('Person');
378: }
379: self::$logger->debug('>>addToGroup(groupName=['.$groupName.'])');
380:
381: $group = new Rights();
382: $group->loadByAttribute('name', $groupName);
383:
384: $lookup = $this->getPropObject('rights')->getLookup();
385: $lookup->setValue(array($this->getOID(), $group->getOID()));
386: $lookup->save();
387:
388: self::$logger->debug('<<addToGroup');
389: }
390:
391: 392: 393: 394: 395: 396: 397: 398: 399: 400:
401: public function sendMail($message, $subject)
402: {
403: $config = ConfigProvider::getInstance();
404:
405: $body = '<html><head></head><body><p>Dear '.$this->getDisplayName().',</p>';
406:
407: $body .= $message;
408:
409: $body .= '</body></html>';
410:
411: $mailer = EmailProviderFactory::getInstance('Alpha\Util\Email\EmailProviderPHP');
412: $mailer->send($this->get('email'), $config->get('email.reply.to'), $subject, $body, true);
413: }
414:
415: 416: 417: 418: 419: 420: 421:
422: public function generatePassword()
423: {
424: $alphabet = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
425:
426: $newPassword = '';
427: for ($i = 0; $i < 7; ++$i) {
428: $newPassword .= $alphabet[rand(0, 25)];
429: }
430: $newPassword .= rand(0, 100);
431: $newPassword .= rand(0, 100);
432:
433: return $newPassword;
434: }
435:
436: 437: 438: 439: 440: 441: 442:
443: public function ()
444: {
445: $temp = new ArticleComment();
446:
447: $sqlQuery = 'SELECT COUNT(OID) AS post_count FROM '.$temp->getTableName()." WHERE created_by='".$this->OID."';";
448:
449: $result = $this->query($sqlQuery);
450:
451: $row = $result[0];
452:
453: if (isset($row['post_count'])) {
454: return $row['post_count'];
455: } else {
456: return 0;
457: }
458: }
459: }
460: