Exemplo n.º 1
0
        /// <summary>
        /// این متد رکورد سازمان مرکزی را به همراه واحد مرکز استان ، احراز هویت ، شخص ، کاربر
        /// و نقش مدیر واحد مرکز استان را ثبت می کند و نقش را به کاربر مربوطه انتساب می دهد
        /// </summary>
        /// <param name="modelRegisterProgram"></param>
        /// <returns></returns>
        public bool RegisterProgramBranchProvince(RegisterProgramBranchProvinceModel modelRegisterProgram)
        {
            try
            {
                //ثبت رکورد سازمان مرکزی
                CentralOrganization centralOrganization = new CentralOrganization
                {
                    Name = "سازمان مرکزی"
                };
                _centralOrganizationRepository.Add(centralOrganization);

                BaseRegisterProgramModel baseRegisterProgramModel = new BaseRegisterProgramModel
                {
                    RoleType     = RoleType.AdminCentral,
                    Name         = "مدیر سازمان",
                    NationalCode = "1111111111",
                    Password     = "******",
                    OrganName    = "سازمان مرکزی",
                    UserName     = "******",
                    LicenceCode  = "1111111111"
                };
                AddUserAdminAutomatic(centralOrganization.Id, AuthenticationType.AdminCentral, RoleType.AdminCentral, baseRegisterProgramModel);

                //ثبت رکورد واحد مرکز استان
                BranchProvince branchProvince = new BranchProvince
                {
                    CentralOrganizationId = centralOrganization.Id,
                    Name    = modelRegisterProgram.OrganName,
                    Address = modelRegisterProgram.Address,
                    Code    = modelRegisterProgram.Code,
                    Phone   = modelRegisterProgram.Phone
                };
                _branchProvinceRepository.Add(branchProvince);

                AddUserAdminAutomatic(branchProvince.Id, AuthenticationType.AdminBranch, RoleType.AdminBranch, modelRegisterProgram);

                _unitOfWork.SaveChanges();
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// این متد رکورد مربوط به کاربر مدیر را در سطوح مختلف
        /// به همراه احراز هویت ، نقش ، شخص ، پروفایل و انتساب نقش به یوزر وارد می کند
        /// </summary>
        /// <param name="levelId"></param>
        /// <param name="authenticationType"></param>
        /// <param name="roleType"></param>
        /// <param name="baseRegisterProgramModel"></param>
        /// <returns></returns>
        public virtual bool AddUserAdminAutomatic(long levelId, AuthenticationType authenticationType,
                                                  RoleType roleType, BaseRegisterProgramModel baseRegisterProgramModel)
        {
            try
            {
                var hasher = new PasswordHasher();

                //ثبت رکورد احراز هویت برای مدیر دانشگاه
                Authentication authentication = new Authentication(levelId, authenticationType,
                                                                   baseRegisterProgramModel.LicenceCode);
                _authenticationRepository.Add(authentication);

                //ثبت رکورد نقش مدیر دانشگاه
                Role role = new Role(levelId, roleType);
                _roleRepository.Add(role);

                //ایجاد کاربر برای مدیر دانشگاه
                User user = new User
                {
                    AuthenticationId = authentication.Id,
                    Email            = baseRegisterProgramModel.UserName,
                    UserName         = baseRegisterProgramModel.UserName,
                    PasswordHash     = hasher.HashPassword(baseRegisterProgramModel.Password),
                    SecurityStamp    = Guid.NewGuid().ToString(),
                    EmailConfirmed   = true
                };

                //ایجاد پروفایل
                Profile profile = new Profile
                {
                    Name         = baseRegisterProgramModel.Name,
                    Family       = baseRegisterProgramModel.Family,
                    NationalCode = baseRegisterProgramModel.NationalCode,
                    Mobile       = baseRegisterProgramModel.Mobile
                };

                // ثبت رکورد جدول شخص و کاربر برای مدیر دانشگاه
                Person person = new Person
                {
                    User    = user,
                    Profile = profile
                };
                if (authenticationType == AuthenticationType.AdminCentral)
                {
                    person.CentralOrganizationId = levelId;
                }
                else if (authenticationType == AuthenticationType.AdminBranch)
                {
                    person.BranchProvinceId = levelId;
                }
                else
                {
                    person.UniversityId = levelId;
                }

                _personRepository.Add(person);

                //انتساب نقش مدیریت به کاربر دانشگاه
                UserRole userRole = new UserRole
                {
                    RoleId = role.Id,
                    UserId = person.Id
                };
                _userRoleRepository.Add(userRole);

                _unitOfWork.SaveChanges();
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }