Example #1
0
        /// <summary>
        /// Prepare researcher model
        /// </summary>
        /// <param name="model">Researcher model</param>
        /// <param name="researcher">Researcher</param>
        /// <param name="excludeProperties">Whether to exclude populating of some properties of model</param>
        /// <returns>Researcher model</returns>
        public virtual ResearcherModel PrepareResearcherModel(ResearcherModel model, Researcher researcher, bool excludeProperties = false)
        {
            if (researcher != null)
            {
                //fill in model values from the entity
                model = model ?? researcher.ToModel <ResearcherModel>();
                model.ResearcherCode   = researcher.ResearcherCode;
                model.TitleId          = researcher.TitleId;
                model.FirstName        = researcher.FirstName;
                model.LastName         = researcher.LastName;
                model.FirstNameEN      = researcher.FirstNameEN;
                model.LastNameEN       = researcher.LastNameEN;
                model.DateOfBirthDay   = researcher.Birthdate?.Day;
                model.DateOfBirthMonth = researcher.Birthdate?.Month;
                model.DateOfBirthYear  = researcher.Birthdate?.Year;
                model.IDCard           = researcher.IDCard;
                model.Telephone        = researcher.Telephone;
                model.Email            = researcher.Email;
                model.PictureId        = researcher.PictureId;
                model.PersonalTypeId   = researcher.PersonalTypeId;
                model.AgencyId         = researcher.AgencyId;
                model.AcademicRankId   = researcher.AcademicRankId;
                model.IsActive         = researcher.IsActive;
                if (researcher.Birthdate.HasValue)
                {
                    model.DateOfBirthDay   = researcher.Birthdate.Value.Day;
                    model.DateOfBirthMonth = researcher.Birthdate.Value.Month;
                    model.DateOfBirthYear  = researcher.Birthdate.Value.Year + 543;
                    model.DateOfBirthName  = CommonHelper.ConvertToThaiDate(researcher.Birthdate.Value);
                }

                PrepareResearcherEducationSearchModel(model.ResearcherEducationSearchModel, researcher);
                model.AcademicRankName             = researcher.AcademicRank != null ? researcher.AcademicRank.NameTh : string.Empty;
                model.PersonalTypeName             = researcher.PersonalType.GetAttributeOfType <EnumMemberAttribute>().Value;
                model.ResearcherEducationListModel = PrepareResearcherEducationListModel(new ResearcherEducationSearchModel {
                    ResearcherId = researcher.Id
                }, researcher);
            }
            else
            {
                model.ResearcherCode = _researcherService.GetNextNumber();
            }
            PrepareAddressModel(model.AddressModel, researcher);
            _baseAdminModelFactory.PrepareTitles(model.AvailableTitles, true, "--ระบุคำนำหน้าชื่อ--");
            _baseAdminModelFactory.PrepareAgencies(model.AvailableAgencies, true, "--ระบุประเภทหน่วยงาน--");
            _baseAdminModelFactory.PreparePersonalTypes(model.AvailablePersonalTypes, true, "--ระบุประเภทบุคลากร--");
            int personType = 1;

            if (model.PersonalTypeId != 0)
            {
                personType = model.PersonalTypeId;
            }
            _baseAdminModelFactory.PrepareAcademicRanks(model.AvailableAcademicRanks, personType, true, "--ระบุตำแหน่งวิชาการ--");

            _baseAdminModelFactory.PrepareDegrees(model.AvailableAddEducationDegrees, true, "--ระบุระดับปริญญา--");
            _baseAdminModelFactory.PrepareEducationLevels(model.AvailableAddEducationEducationLevels, true, "--ระบุวุฒิการศึกษา--");
            _baseAdminModelFactory.PrepareInstitutes(model.AvailableAddEducationInstitutes, true, "--ระบุสถาบันการศึกษา--");
            _baseAdminModelFactory.PrepareCountries(model.AvailableAddEducationCountries, true, "--ระบุประเทศ--");
            //Default Thailand
            model.AddEducationCountryId      = 229;
            model.AddEducationGraduationYear = DateTime.Now.Year + 543;
            return(model);
        }
        /// <summary>
        /// Register user
        /// </summary>
        /// <param name="request">Request</param>
        /// <returns>Result</returns>
        public virtual UserRegistrationResult RegisterUser(UserRegistrationRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.User == null)
            {
                throw new ArgumentException("Can't load current user");
            }

            var result = new UserRegistrationResult();

            if (request.User.IsSearchEngineAccount())
            {
                result.AddError("Search engine can't be registered");
                return(result);
            }

            if (request.User.IsBackgroundTaskAccount())
            {
                result.AddError("Background task account can't be registered");
                return(result);
            }

            if (request.User.IsRegistered())
            {
                result.AddError("Current user is already registered");
                return(result);
            }

            if (string.IsNullOrEmpty(request.Email))
            {
                result.AddError("EmailIsNotProvided");
                return(result);
            }

            if (!CommonHelper.IsValidEmail(request.Email))
            {
                result.AddError("WrongEmail");
                return(result);
            }

            if (string.IsNullOrWhiteSpace(request.Password))
            {
                result.AddError("Password Is Not Provided");
                return(result);
            }

            if (_userSettings.UsernamesEnabled && string.IsNullOrEmpty(request.Username))
            {
                result.AddError("Username Is Not Provided");
                return(result);
            }

            //validate unique user
            if (_userService.GetUserByEmail(request.Email) != null)
            {
                result.AddError("Email Already Exists");
                return(result);
            }

            //at this point request is valid
            request.User.UserName = request.Username;
            request.User.Email    = request.Email;

            var userPassword = new UserPassword
            {
                UserId         = request.User.Id,
                PasswordFormat = request.PasswordFormat,
                CreatedOnUtc   = DateTime.UtcNow
            };

            switch (request.PasswordFormat)
            {
            case PasswordFormat.Clear:
                userPassword.Password = request.Password;
                break;

            case PasswordFormat.Encrypted:
                userPassword.Password = _encryptionService.EncryptText(request.Password);
                break;

            case PasswordFormat.Hashed:
                var saltKey = _encryptionService.CreateSaltKey(ResearchUserServiceDefaults.PasswordSaltKeySize);
                userPassword.PasswordSalt = saltKey;
                userPassword.Password     = _encryptionService.CreatePasswordHash(request.Password, saltKey, _userSettings.HashedPasswordFormat);
                break;
            }

            _userService.InsertUserPassword(userPassword);
            //add to 'researcherRole' role
            var researcherRole = _userService.GetUserRoleBySystemName(ResearchUserDefaults.ResearchersRoleName);

            if (researcherRole == null)
            {
                throw new ResearchException("'Researchers' role could not be loaded");
            }

            request.User.UserUserRoleMappings.Add(new UserUserRoleMapping {
                UserRole = researcherRole
            });
            //remove from 'Guests' role
            var guestRole = request.User.UserRoles.FirstOrDefault(cr => cr.SystemName == ResearchUserDefaults.GuestsRoleName);

            if (guestRole != null)
            {
                request.User.UserUserRoleMappings
                .Remove(request.User.UserUserRoleMappings.FirstOrDefault(mapping => mapping.UserRoleId == guestRole.Id));
            }
            //_userService.UpdateUser(request.User);
            var researcher = new Researcher
            {
                ResearcherCode = _researcherService.GetNextNumber(),
                Email          = request.User.Email,
                AgencyId       = request.User.AgencyId,
                FirstName      = request.User.FirstName,
                LastName       = request.User.LastName,
                IDCard         = request.IDCard,
                TitleId        = request.User.TitleId,
                Gender         = request.Gender,
                IsActive       = true,
                PersonalTypeId = (int)PersonalType.Academic,
            };

            _researcherService.InsertResearcher(researcher);
            request.User.ResearcherId = researcher.Id;
            request.User.Roles        = researcherRole.SystemName;
            _userService.UpdateUser(request.User);
            //_workContext.CurrentUser = request.User;

            return(result);
        }