Exemple #1
0
        public Task CreateAsync(IdentityUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var coreUser = new User()
            {
                Username         = user.UserName,
                Email            = user.Email,
                Password         = user.PasswordHash.ToString(),
                UserType         = user.UserType,
                PasswordKey      = user.SecurityStamp,
                IsFirstTimeUser  = user.IsFirstTimeUser,
                IsApproved       = user.IsApproved,
                ProfilePhotoName = user.ProfilePhotoName
            };

            _coreRepository.SaveOrUpdate(coreUser);

            return(Task.FromResult <object>(null));
        }
Exemple #2
0
        public object ToggleStudentActivation(int studentId)
        {
            var student = _coreRepository.Get <Student>(studentId);

            if (student != null)
            {
                var isLockedOut = student.User.IsLockedOut;
                if (isLockedOut)
                {
                    student.User.IsLockedOut = false;
                }
                else
                {
                    student.User.IsLockedOut      = true;
                    student.User.LastActivityDate = DateTime.Now;
                }
                var isToggled = _coreRepository.SaveOrUpdate(student);
                return(isToggled);
            }
            return(false);
        }
Exemple #3
0
        public ActionResult EditStaffProfile(EditPerson model)
        {
            if (ModelState.IsValid)
            {
                var staffToEdit = _coreRepo.Get <Instructor>(model.ParentClassId);

                if (staffToEdit != null)
                {
                    staffToEdit.Person.Title = model.Title;

                    staffToEdit.Person.Gender        = model.Gender;
                    staffToEdit.Person.DateOfBirth   = model.DateOfBirth;
                    staffToEdit.Person.MaritalStatus = model.MaritalStatus;
                    staffToEdit.Person.Religion      = model.Religion;

                    //SET ALL COUNTRIES
                    var nationality =
                        _coreRepository.GetAll <Country>()
                        .FirstOrDefault(x => x.Id.Equals(model.NationalityId));

                    staffToEdit.Person.Country2 = nationality;
                    staffToEdit.Person.Country  = nationality;
                    staffToEdit.Person.Country1 = nationality;

                    staffToEdit.Person.PlaceOfBirth = nationality != null ? nationality.Name : null;

                    string newFileName = null;
                    if (model.ProfilePhotoFile != null)
                    {
                        if (_generalHelper.SaveFile(model.ProfilePhotoFile,
                                                    ApplicationConstants.StaffResourcesFolder,
                                                    out newFileName, model.ProfilePhotoName))
                        {
                            _generalHelper.ResizeImage(model.ProfilePhotoFile, ResourceFolders.ThumbnailSmall,
                                                       ApplicationConstants.StaffResourcesFolder, newFileName, model.ProfilePhotoName);
                            _generalHelper.ResizeImage(model.ProfilePhotoFile, ResourceFolders.Thumbnail,
                                                       ApplicationConstants.StaffResourcesFolder, newFileName, model.ProfilePhotoName);
                            staffToEdit.Person.ProfilePhotoName = newFileName;
                            staffToEdit.User.ProfilePhotoName   = newFileName;
                        }
                    }
                    if (!string.IsNullOrEmpty(model.TelephoneContact))
                    {
                        staffToEdit.Person.TelephoneContact = model.TelephoneContact;
                    }
                    if (!string.IsNullOrEmpty(model.AltTelephoneContact))
                    {
                        staffToEdit.Person.AltTelephoneContact = model.AltTelephoneContact;
                    }
                    if (!string.IsNullOrEmpty(model.EmailAddress))
                    {
                        staffToEdit.Person.EmailAddress = model.EmailAddress;
                    }
                    if (!string.IsNullOrEmpty(model.AltEmailAddress))
                    {
                        staffToEdit.Person.AltEmailAddress = model.AltEmailAddress;
                    }
                    if (!string.IsNullOrEmpty(model.NextOfKinName))
                    {
                        staffToEdit.Person.NextOfKinName = model.NextOfKinName;
                    }
                    if (!string.IsNullOrEmpty(model.NextOfKinRelationship))
                    {
                        staffToEdit.Person.NextOfKinRelationship = model.NextOfKinRelationship;
                    }
                    if (!string.IsNullOrEmpty(model.NextOfKinContact))
                    {
                        staffToEdit.Person.NextOfKinContact = model.NextOfKinContact;
                    }
                    //Default the occupation to Staff
                    staffToEdit.Person.Occupation = "Staff";

                    if (!string.IsNullOrEmpty(model.PostalAddress))
                    {
                        staffToEdit.Person.PostalAddress = model.PostalAddress;
                    }

                    staffToEdit.Person.PersonOwnerType = int.Parse(ApplicationConstants.StaffUserType);
                    DateTime savedDateTime = DateTime.Now;
                    staffToEdit.Person.LastModified = savedDateTime;

                    var isSaved = _coreRepo.SaveOrUpdate(staffToEdit);
                    if (isSaved)
                    {
                        if (!string.IsNullOrEmpty(newFileName))
                        {
                            TempData[ApplicationConstants.SuccessNotification] = "The profile photo has been updated. Log out and log back in to view changes!";
                        }
                        return(RedirectToAction("Index", "Staff"));
                    }
                    else
                    {
                        ModelState.AddModelError("Save Fail", "Saving of Edit failed");
                    }
                }
                else
                {
                    ModelState.AddModelError("StaffNone", "Staff Member doesn't exist");
                }
            }
            //Listing errors
            foreach (string error in _generalHelper.GetErrorList(ModelState))
            {
                ModelState.AddModelError("", error);
            }

            model.CountryOptions = new SelectList(_coreRepo.GetAll <Country>().OrderByDescending(x => x.Id), "Id", "Name").ToList();
            return(View(model));
        }
Exemple #4
0
        public ActionResult StaffPersonalDetails(UserInfoViewModel model, string buttonClicked, HttpPostedFileBase file)
        {
            try
            {
                //When no staff details exist i.e Add new staff details
                if (model.User.Id <= 0 && model.Person.Surname == null)
                {
                    ViewBag.CountryId = ViewBag.CountryId ?? new SelectList(_coreRepo.GetAll <Country>(), "Id", "Name", model);

                    return(PartialView("_editStaffPersonalDetails", model));
                }

                //if (model.User.Id > 0)
                //{
                //Check whether username already exists
                if (model.User.Id <= 0 && _coreRepo.Get <User>("Username", model.User.Username) != null)
                {
                    ModelState.AddModelError("Error", "The username chosen already exists!");
                    ViewBag.CountryId = ViewBag.CountryId ??
                                        new SelectList(_coreRepo.GetAll <Country>(), "Id", "Name", model);

                    return(PartialView("_editStaffPersonalDetails", model));
                }

                var user = _coreRepo.Get <User>(model.User.Id);

                //Retrieving Existing Staff Member
                Instructor staff = _staffRepo.GetStaffByUserId(model.User.Id);

                if (buttonClicked == "Save")
                {
                    //Saving or Updating Person
                    Person person = staff != null ? staff.Person : new Person();

                    person.Title         = model.Person.Title;
                    person.Surname       = model.Person.Surname;
                    person.GivenName     = model.Person.GivenName;
                    person.OtherName     = model.Person.OtherName;
                    person.Gender        = model.Person.Gender;
                    person.DateOfBirth   = model.Person.DateOfBirth;
                    person.MaritalStatus = model.Person.MaritalStatus;
                    person.Religion      = model.Person.Religion;
                    person.Country2      = model.Person.Country2 != null ? model.Person.Country2.Id > 0 ? _coreRepo.Get <Country>(model.Person.Country2.Id) : model.Person.Country : null;
                    person.Country       = model.Person.Country2 != null ? model.Person.Country2.Id > 0 ? _coreRepo.Get <Country>(model.Person.Country2.Id) : model.Person.Country : null;
                    person.Country1      = model.Person.Country2 != null ? model.Person.Country2.Id > 0 ? _coreRepo.Get <Country>(model.Person.Country2.Id) : model.Person.Country : null;
                    person.PlaceOfBirth  = model.Person.Country2 != null ? model.Person.Country2.Id > 0 ? _coreRepo.Get <Country>(model.Person.Country2.Id).Name : null
                        : null;

                    string newFileName = null;
                    if (file != null)
                    {
                        if (_generalHelper.SaveFile(file, ApplicationConstants.StaffResourcesFolder,
                                                    out newFileName, model.User.ProfilePhotoName))
                        {
                            _generalHelper.ResizeImage(file, ResourceFolders.ThumbnailSmall,
                                                       ApplicationConstants.StaffResourcesFolder, newFileName, !string.IsNullOrEmpty(model.User.ProfilePhotoName)? model.User.ProfilePhotoName : string.IsNullOrEmpty(model.Person.ProfilePhotoName) ? null : model.Person.ProfilePhotoName);
                        }
                        _generalHelper.ResizeImage(file, ResourceFolders.Thumbnail,
                                                   ApplicationConstants.StaffResourcesFolder, newFileName, !string.IsNullOrEmpty(model.User.ProfilePhotoName) ? model.User.ProfilePhotoName : string.IsNullOrEmpty(model.Person.ProfilePhotoName) ? null : model.Person.ProfilePhotoName);
                        model.User.ProfilePhotoName = newFileName;
                        person.ProfilePhotoName     = newFileName;
                    }

                    person.PostalAddress         = model.Person.PostalAddress;
                    person.EmailAddress          = model.Person.EmailAddress;
                    person.AltEmailAddress       = model.Person.AltEmailAddress;
                    person.TelephoneContact      = model.Person.TelephoneContact;
                    person.NextOfKinName         = model.Person.NextOfKinName;
                    person.NextOfKinRelationship = model.Person.NextOfKinRelationship;
                    person.NextOfKinContact      = model.Person.NextOfKinContact;
                    person.NextOfKinAddress      = model.Person.NextOfKinAddress;
                    person.WebsiteUrl            = model.Person.WebsiteUrl;
                    person.PersonOwnerType       = int.Parse(ApplicationConstants.StaffUserType);
                    person.LastModified          = DateTime.Now;

                    if (model.User.Id <= 0)
                    {
                        person.CreatedOn = DateTime.Now;
                    }

                    //now saving User Information
                    if (_coreRepo.SaveOrUpdate(person))
                    {
                        //Saving or Updating the User Information in the User Table
                        User userAccount = user ?? new User();
                        userAccount.Username        = model.User.Username;
                        userAccount.Email           = person.EmailAddress;
                        userAccount.UserType        = int.Parse(ApplicationConstants.StaffUserType);
                        userAccount.IsApproved      = true;
                        userAccount.IsLockedOut     = false;
                        userAccount.IsFirstTimeUser = user == null;

                        if (!string.IsNullOrEmpty(newFileName))
                        {
                            userAccount.ProfilePhotoName = newFileName;
                        }

                        if (_coreRepo.SaveOrUpdate(userAccount))
                        {
                            staff = staff ?? new Instructor();

                            staff.Person    = person;
                            staff.User      = userAccount;
                            staff.CreatedOn = DateTime.Now;

                            if (_coreRepo.SaveOrUpdate(staff))
                            {
                                //Set Password to Default Password if new user
                                var userAccountSaved = _coreRepo.Get <User>(userAccount.Id);
                                if (userAccountSaved != null && userAccountSaved.Password == null &&
                                    userAccountSaved.PasswordKey == null)
                                {
                                    IdentityResult result = UserManager.AddPassword(userAccountSaved.Id,
                                                                                    ApplicationConstants.DEFAULT_STUDENT_PASSWORD);
                                    if (result.Succeeded)
                                    {
                                        ;
                                    }
                                }
                            }

                            //Redirect so as to refresh the view details
                            return(JavaScript("window.location = '" + Url.Action("StaffDetails", "UserManagement", new { id = userAccount.Id }) + "'"));
                        }
                    }
                }
                if (user != null)
                {
                    staff        = _staffRepo.GetStaffByUserId(model.User.Id);
                    model.User   = user;
                    model.Person = staff.Person;

                    if (model.Person.ProfilePhotoName != null)
                    {
                        model.Person.ProfilePhotoUrl = Path.Combine(ApplicationConstants.StaffResourcesThumbnailUrl,
                                                                    staff.Person.ProfilePhotoName);
                    }
                    else if (model.User.ProfilePhotoName != null)
                    {
                        model.Person.ProfilePhotoUrl = Path.Combine(ApplicationConstants.StaffResourcesThumbnailUrl,
                                                                    staff.User.ProfilePhotoName);
                    }
                    else
                    {
                        model.Person.ProfilePhotoUrl = ApplicationConstants.ProfilePhotoAvartarUrl;
                    }
                    ViewBag.CountryId = ViewBag.CountryId ??
                                        new SelectList(_coreRepo.GetAll <Country>(), "Id", "Name", model);
                }

                if (buttonClicked == "Edit")
                {
                    ViewBag.CountryId = ViewBag.CountryId ?? new SelectList(_coreRepo.GetAll <Country>(), "Id", "Name", model);

                    return(PartialView("_editStaffPersonalDetails", model));
                }
                //}
                return(PartialView("_StaffPersonalDetails", model));
            }
            catch (Exception e)
            {
                throw e;
            }
        }