Ejemplo n.º 1
0
 /**
  * If any role has no permission records, set Member-like permissions on it.
  *
  * @return array
  */
 public function FixPermissions()
 {
     $Roles = RoleModel::Roles();
     $RoleModel = new RoleModel();
     $PermissionModel = new PermissionModel();
     // Find roles missing permission records
     foreach ($Roles as $RoleID => $Role) {
         $Permissions = $this->SQL->Select('*')->From('Permission p')->Where('p.RoleID', $RoleID)->Get()->ResultArray();
         if (!count($Permissions)) {
             // Set basic permission record
             $DefaultRecord = array('RoleID' => $RoleID, 'JunctionTable' => null, 'JunctionColumn' => null, 'JunctionID' => null, 'Garden.Email.View' => 1, 'Garden.SignIn.Allow' => 1, 'Garden.Activity.View' => 1, 'Garden.Profiles.View' => 1, 'Garden.Profiles.Edit' => 1, 'Conversations.Conversations.Add' => 1);
             $PermissionModel->Save($DefaultRecord);
             // Set default category permission
             $DefaultCategory = array('RoleID' => $RoleID, 'JunctionTable' => 'Category', 'JunctionColumn' => 'PermissionCategoryID', 'JunctionID' => -1, 'Vanilla.Discussions.View' => 1, 'Vanilla.Discussions.Add' => 1, 'Vanilla.Comments.Add' => 1);
             $PermissionModel->Save($DefaultCategory);
         }
     }
     return array('Complete' => TRUE);
 }
 /**
  * Generic save procedure.
  */
 public function Save($FormPostValues, $Settings = FALSE)
 {
     // See if the user's related roles should be saved or not.
     $SaveRoles = GetValue('SaveRoles', $Settings);
     // Define the primary key in this model's table.
     $this->DefineSchema();
     // Custom Rule: This will make sure that at least one role was selected if saving roles for this user.
     if ($SaveRoles) {
         $this->Validation->AddRule('OneOrMoreArrayItemRequired', 'function:ValidateOneOrMoreArrayItemRequired');
         // $this->Validation->AddValidationField('RoleID', $FormPostValues);
         $this->Validation->ApplyRule('RoleID', 'OneOrMoreArrayItemRequired');
     }
     // Make sure that the checkbox val for email is saved as the appropriate enum
     if (array_key_exists('ShowEmail', $FormPostValues)) {
         $FormPostValues['ShowEmail'] = ForceBool($FormPostValues['ShowEmail'], '0', '1', '0');
     }
     if (array_key_exists('Banned', $FormPostValues)) {
         $FormPostValues['Banned'] = ForceBool($FormPostValues['Banned'], '0', '1', '0');
     }
     // Validate the form posted values
     $UserID = GetValue('UserID', $FormPostValues);
     $Insert = $UserID > 0 ? FALSE : TRUE;
     if ($Insert) {
         $this->AddInsertFields($FormPostValues);
     } else {
         $this->AddUpdateFields($FormPostValues);
     }
     $this->EventArguments['FormPostValues'] = $FormPostValues;
     $this->FireEvent('BeforeSaveValidation');
     $RecordRoleChange = TRUE;
     if ($UserID && GetValue('FixUnique', $Settings)) {
         $UniqueValid = $this->ValidateUniqueFields(GetValue('Name', $FormPostValues), GetValue('Email', $FormPostValues), $UserID, TRUE);
         if (!$UniqueValid['Name']) {
             unset($FormPostValues['Name']);
         }
         if (!$UniqueValid['Email']) {
             unset($FormPostValues['Email']);
         }
         $UniqueValid = TRUE;
     } else {
         $UniqueValid = $this->ValidateUniqueFields(GetValue('Name', $FormPostValues), GetValue('Email', $FormPostValues), $UserID);
     }
     // Add & apply any extra validation rules:
     if (array_key_exists('Email', $FormPostValues) && GetValue('ValidateEmail', $Settings, TRUE)) {
         $this->Validation->ApplyRule('Email', 'Email');
     }
     if ($this->Validate($FormPostValues, $Insert) && $UniqueValid) {
         $Fields = $this->Validation->ValidationFields();
         // All fields on the form that need to be validated (including non-schema field rules defined above)
         $RoleIDs = GetValue('RoleID', $Fields, 0);
         $Username = GetValue('Name', $Fields);
         $Email = GetValue('Email', $Fields);
         $Fields = $this->Validation->SchemaValidationFields();
         // Only fields that are present in the schema
         // Remove the primary key from the fields collection before saving
         $Fields = RemoveKeyFromArray($Fields, $this->PrimaryKey);
         if (in_array('AllIPAddresses', $Fields) && is_array($Fields)) {
             $Fields['AllIPAddresses'] = implode(',', $Fields['AllIPAddresses']);
         }
         if (!$Insert && array_key_exists('Password', $Fields)) {
             // Encrypt the password for saving only if it won't be hashed in _Insert()
             $PasswordHash = new Gdn_PasswordHash();
             $Fields['Password'] = $PasswordHash->HashPassword($Fields['Password']);
             $Fields['HashMethod'] = 'Vanilla';
         }
         // Check for email confirmation.
         if (self::RequireConfirmEmail() && !GetValue('NoConfirmEmail', $Settings)) {
             if (isset($Fields['Email']) && $UserID == Gdn::Session()->UserID && $Fields['Email'] != Gdn::Session()->User->Email && !Gdn::Session()->CheckPermission('Garden.Users.Edit')) {
                 $User = Gdn::Session()->User;
                 $Attributes = Gdn::Session()->User->Attributes;
                 $ConfirmEmailRoleID = C('Garden.Registration.ConfirmEmailRole');
                 if (RoleModel::Roles($ConfirmEmailRoleID)) {
                     // The confirm email role is set and it exists so go ahead with the email confirmation.
                     $EmailKey = TouchValue('EmailKey', $Attributes, RandomString(8));
                     if (isset($Attributes['ConfirmedEmailRoles']) && !in_array($ConfirmEmailRoleID, $Attributes['ConfirmedEmailRoles'])) {
                         $ConfirmedEmailRoles = $Attributes['ConfirmedEmailRoles'];
                     } elseif ($RoleIDs) {
                         $ConfirmedEmailRoles = $RoleIDs;
                     } else {
                         $ConfirmedEmailRoles = ConsolidateArrayValuesByKey($this->GetRoles($UserID), 'RoleID');
                     }
                     $Attributes['ConfirmedEmailRoles'] = $ConfirmedEmailRoles;
                     $RoleIDs = (array) C('Garden.Registration.ConfirmEmailRole');
                     $SaveRoles = TRUE;
                     $Fields['Attributes'] = serialize($Attributes);
                 }
             }
         }
         $this->EventArguments['Fields'] = $Fields;
         $this->FireEvent('BeforeSave');
         // Check the validation results again in case something was added during the BeforeSave event.
         if (count($this->Validation->Results()) == 0) {
             // If the primary key exists in the validated fields and it is a
             // numeric value greater than zero, update the related database row.
             if ($UserID > 0) {
                 // If they are changing the username & email, make sure they aren't
                 // already being used (by someone other than this user)
                 if (ArrayValue('Name', $Fields, '') != '' || ArrayValue('Email', $Fields, '') != '') {
                     if (!$this->ValidateUniqueFields($Username, $Email, $UserID)) {
                         return FALSE;
                     }
                 }
                 if (array_key_exists('Attributes', $Fields) && !is_string($Fields['Attributes'])) {
                     $Fields['Attributes'] = serialize($Fields['Attributes']);
                 }
                 $this->SQL->Put($this->Name, $Fields, array($this->PrimaryKey => $UserID));
                 // Record activity if the person changed his/her photo.
                 $Photo = ArrayValue('Photo', $FormPostValues);
                 if ($Photo !== FALSE) {
                     if (GetValue('CheckExisting', $Settings)) {
                         $User = $this->GetID($UserID);
                         $OldPhoto = GetValue('Photo', $User);
                     }
                     if (isset($OldPhoto) && $OldPhoto != $Photo) {
                         if (strpos($Photo, '//')) {
                             $PhotoUrl = $Photo;
                         } else {
                             $PhotoUrl = Gdn_Upload::Url(ChangeBasename($Photo, 'n%s'));
                         }
                         $ActivityModel = new ActivityModel();
                         if ($UserID == Gdn::Session()->UserID) {
                             $HeadlineFormat = T('HeadlineFormat.PictureChange', '{RegardingUserID,You} changed {ActivityUserID,your} profile picture.');
                         } else {
                             $HeadlineFormat = T('HeadlineFormat.PictureChange.ForUser', '{RegardingUserID,You} changed the profile picture for {ActivityUserID,user}.');
                         }
                         $ActivityModel->Save(array('ActivityUserID' => $UserID, 'RegardingUserID' => Gdn::Session()->UserID, 'ActivityType' => 'PictureChange', 'HeadlineFormat' => $HeadlineFormat, 'Story' => Img($PhotoUrl, array('alt' => T('Thumbnail')))));
                     }
                 }
             } else {
                 $RecordRoleChange = FALSE;
                 if (!$this->ValidateUniqueFields($Username, $Email)) {
                     return FALSE;
                 }
                 // Define the other required fields:
                 $Fields['Email'] = $Email;
                 $Fields['Roles'] = $RoleIDs;
                 // Make sure that the user is assigned to one or more roles:
                 $SaveRoles = FALSE;
                 // And insert the new user.
                 $UserID = $this->_Insert($Fields, $Settings);
                 if ($UserID) {
                     // Report that the user was created.
                     $ActivityModel = new ActivityModel();
                     $ActivityModel->Save(array('ActivityType' => 'Registration', 'ActivityUserID' => $UserID, 'HeadlineFormat' => T('HeadlineFormat.Registration', '{ActivityUserID,You} joined.'), 'Story' => T('Welcome Aboard!')), FALSE, array('GroupBy' => 'ActivityTypeID'));
                     // Report the creation for mods.
                     $ActivityModel->Save(array('ActivityType' => 'Registration', 'ActivityUserID' => Gdn::Session()->UserID, 'RegardingUserID' => $UserID, 'NotifyUserID' => ActivityModel::NOTIFY_MODS, 'HeadlineFormat' => T('HeadlineFormat.AddUser', '{ActivityUserID,user} added an account for {RegardingUserID,user}.')));
                 }
             }
             // Now update the role settings if necessary.
             if ($SaveRoles) {
                 // If no RoleIDs were provided, use the system defaults
                 if (!is_array($RoleIDs)) {
                     $RoleIDs = Gdn::Config('Garden.Registration.DefaultRoles');
                 }
                 $this->SaveRoles($UserID, $RoleIDs, $RecordRoleChange);
             }
             // Send the confirmation email.
             if (isset($EmailKey)) {
                 $this->SendEmailConfirmationEmail((array) Gdn::Session()->User);
             }
             $this->EventArguments['UserID'] = $UserID;
             $this->FireEvent('AfterSave');
         } else {
             $UserID = FALSE;
         }
     } else {
         //         decho($this->Validation->ResultsText());
         $UserID = FALSE;
     }
     // Clear cached user data
     if (!$Insert && $UserID) {
         $this->ClearCache($UserID, array('user'));
     }
     return $UserID;
 }
Ejemplo n.º 3
0
 /**
  *
  * @param array|string $Names 
  */
 public static function GetByName($Names, &$Missing = NULL)
 {
     if (is_string($Names)) {
         $Names = explode(',', $Names);
         $Names = array_map('trim', $Names);
     }
     // Make a lookup array of the names.
     $Names = array_unique($Names);
     $Names = array_combine($Names, $Names);
     $Names = array_change_key_case($Names);
     $Roles = RoleModel::Roles();
     $Result = array();
     foreach ($Roles as $RoleID => $Role) {
         $Name = strtolower($Role['Name']);
         if (isset($Names[$Name])) {
             $Result[$RoleID] = $Role;
             unset($Names[$Name]);
         }
     }
     $Missing = array_values($Names);
     return $Result;
 }