public Tuple <bool, string> AddOrUpdateAuthentication(AuthenticationModel authenticationModel, StateOperation stateOperation)
 {
     try
     {
         if (stateOperation == StateOperation.درج)
         {
             var authentication = _authenticationRepository.Find(x => x.IdentityCode == authenticationModel.IdentityCode);
             if (authentication != null)
             {
                 return(new Tuple <bool, string>(false, "خطا در انجام عملیات : کد شناسایی مورد نظر تکراری می باشد"));
             }
             Authentication newAuthentication = new Authentication
             {
                 AuthenticationType    = authenticationModel.AuthenticationType,
                 IdentityCode          = authenticationModel.IdentityCode,
                 CentralOrganizationId = authenticationModel.CentralOrganizationId,
                 BranchProvinceId      = authenticationModel.BranchProvinceId,
                 UniversityId          = authenticationModel.UniversityId
             };
             _authenticationRepository.Add(newAuthentication);
         }
         else
         {
             if (_authenticationRepository.Contains(x => x.Id != authenticationModel.Id && x.IdentityCode == authenticationModel.IdentityCode))
             {
                 return(new Tuple <bool, string>(false, "خطا در انجام عملیات : کد شناسایی مورد نظر تکراری می باشد"));
             }
             var authentication = _authenticationRepository.Find(x => x.Id == authenticationModel.Id);
             if (authentication == null)
             {
                 return(new Tuple <bool, string>(false, "خطا در انجام عملیات : رکورد مورد نظر یافت نشد"));
             }
             authentication.IdentityCode       = authenticationModel.IdentityCode;
             authentication.AuthenticationType = authenticationModel.AuthenticationType;
             //authentication.CentralOrganizationId = authenticationModel.CentralOrganizationId;
             //authentication.BranchProvinceId = authenticationModel.BranchProvinceId;
             //authentication.UniversityId = authenticationModel.UniversityId;
             _authenticationRepository.Update(authentication);
         }
         _unitOfWork.SaveChanges();
         return(new Tuple <bool, string>(true, "عملیات ثبت شد"));
     }
     catch (Exception ex)
     {
         return(new Tuple <bool, string>(false, "خطا در انجام عملیات"));
     }
 }
示例#2
0
        /// <summary>
        /// رکورد شخص و کاربر و پروفایل و دانشجو یا پرسنل ذخیره شده و همچنین سطح مربوط به تعریف شخص هم مشخص شده است
        /// </summary>
        /// <param name="identificationCode"></param>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <param name="nationalCode"></param>
        /// <param name="mobile"></param>
        /// <param name="name"></param>
        /// <param name="family"></param>
        /// <returns></returns>
        public virtual async Task <Tuple <bool, string, string, User> > AddUser(string identificationCode, string email, string password,
                                                                                string name, string family, string nationalCode, string mobile)
        {
            try
            {
                using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    //_unitOfWork.BeginTransaction();
                    //var hasher = new PasswordHasher();

                    //واکشی احراز هویت مربوط به کد شناسایی
                    var authentication =
                        _authenticationRepository.Where(a => a.IdentityCode == identificationCode)
                        .Include(i => i.Users).FirstOrDefault();
                    if (authentication == null)
                    {
                        return(new Tuple <bool, string, string, User>(false, "اطلاعات پایه کاربری وجود ندارد!", "", null));
                    }
                    if (authentication.Users.Any())
                    {
                        return(new Tuple <bool, string, string, User>(false, "اطلاعات پایه کاربری قبلا استفاده شده است!", "", null));
                    }

                    //ایجاد پروفایل
                    var profile = new Profile
                    {
                        Name         = name,
                        Family       = family,
                        NationalCode = nationalCode,
                        Mobile       = mobile
                    };

                    ////رکورد مربوط به دانشجو یا پرسنل
                    //switch (authentication.AuthenticationType)
                    //{
                    //    case AuthenticationType.Personel:
                    //        Personel personel = new Personel();
                    //        person.Personel = personel;
                    //        break;
                    //    case AuthenticationType.Student:
                    //        Student student = new Student();
                    //        person.Student = student;
                    //        break;
                    //}

                    //انتساب سطح تعریف شده مربوط به جدول احراز هویت به جدول شخص و ایجاد شخص
                    var person = new Person {
                        Profile = profile
                    };
                    switch (authentication.AuthenticationType)
                    {
                    case AuthenticationType.AdminCentral:
                        person.CentralOrganizationId = authentication.CentralOrganizationId;
                        break;

                    case AuthenticationType.AdminBranch:
                        person.BranchProvinceId = authentication.BranchProvinceId;
                        break;

                    default:
                        person.UniversityId = authentication.UniversityId;
                        break;
                    }
                    _personRepository.Add(person);


                    // ویرایش احراز هوییت بخش پر کردن پروفایل
                    var objectauthentication = _authenticationRepository.Find(p => p.IdentityCode == identificationCode);
                    objectauthentication.ExistinProfile = true;

                    // _authenticationRepository.Update(authentication);

                    _unitOfWork.SaveChanges();
                    //ایجاد کاربر
                    var user = new User
                    {
                        Id = person.Id,
                        AuthenticationId = authentication.Id,
                        Email            = email,
                        UserName         = email
                                           //PasswordHash = hasher.HashPassword(registerUser.Password)
                    };
                    //PasswordHasher hashe = new PasswordHasher();

                    var result = await _userManager.CreateAsync(user, password);

                    if (!result.Succeeded)
                    {
                        return(new Tuple <bool, string, string, User>(false, "خطا در ثبت نام کاربر!", "", null));
                    }
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    //_unitOfWork.CommitTransaction();
                    scope.Complete();
                    return(new Tuple <bool, string, string, User>(true, "", code, user));
                    //_unitOfWork.Rollback();
                }
            }
            catch (Exception ex)
            {
                //_unitOfWork.Rollback();
                return(new Tuple <bool, string, string, User>(false, "خطا در ثبت نام کاربر!", "", null));
            }
        }