Beispiel #1
0
        public async Task <HttpResponseMessage> GetUserInfoByCodeMeli([FromUri] string codeMeli)
        {
            try
            {
                //SecurityManager.ThrowIfUserContextNull();
                //if (!SecurityManager.CurrentUserContext.IsOrganizationPersonnel() && !SecurityManager.CurrentUserContext.IsDeveloperUser() &&
                //    !SecurityManager.CurrentUserContext.IsAdminUser() && !SecurityManager.CurrentUserContext.IsSupplyChainUser())
                //    throw new Exception(VBSConstants.IlegalAccess);

                ZhivarUserInfoRule userInfoRule = new ZhivarUserInfoRule(Rule.OperationAccess, Rule.UnitOfWork);
                bool status = await userInfoRule.Queryable().AnyAsync(user => user.NationalCode == codeMeli);

                return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = (int)ResultCode.Successful, data = status }));
            }
            catch (Exception ex)
            {
                return(await this.HandleExceptionAsync(ex));
            }
        }
Beispiel #2
0
        public async Task <HttpResponseMessage> LoadPersonel([FromBody] PersonelRequest request)
        {
            try
            {
                SecurityManager.ThrowIfUserContextNull();
                //if (!SecurityManager.CurrentUserContext.IsOrganizationPersonnel() && !SecurityManager.CurrentUserContext.IsDeveloperUser() &&
                //    !SecurityManager.CurrentUserContext.IsAdminUser() && !SecurityManager.CurrentUserContext.IsSupplyChainUser())
                //    throw new Exception(VBSConstants.IlegalAccess);

                PersonelVM personelVM = new PersonelVM();
                Personel   personel   = await Rule.GetPersonelInfo(request);

                if (personel != null)
                {
                    personelVM = personel.Translate <PersonelVM>();
                    //if (personel.SignatureContent != null)
                    //    personelVM.FileSpaceBase64 = string.Format(@"data:image/jpeg;base64,{0}", Convert.ToBase64String(personel.SignatureContent));

                    ZhivarUserInfoRule userInfoRule = new ZhivarUserInfoRule(Rule.OperationAccess, Rule.UnitOfWork);
                    var userInfo = userInfoRule.Queryable()
                                   .Where(u => u.ID == personelVM.UserID)
                                   .Select(u => new { u.UserName, u.Tag5 })
                                   .SingleOrDefault();

                    if (userInfo != null)
                    {
                        personelVM.NeedToSaveUser = true;
                        personelVM.UserName       = userInfo.UserName;
                        personelVM.NeedToSign     = userInfo.Tag5 == null ? 2 : userInfo.Tag5.ConvertTo <int>();
                    }
                }

                return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = (int)ResultCode.Successful, data = personelVM }));
            }
            catch (Exception ex)
            {
                return(await this.HandleExceptionAsync(ex));
            }
        }
Beispiel #3
0
        //[HttpPost]
        //public async Task<HttpResponseMessage> ChangeStatusAsync([FromBody]int personnelId)
        //{
        //    try
        //    {
        //        SecurityManager.ThrowIfUserContextNull();
        //        if (!SecurityManager.CurrentUserContext.IsOrganizationPersonnel() && !SecurityManager.CurrentUserContext.IsDeveloperUser() &&
        //            !SecurityManager.CurrentUserContext.IsAdminUser() && !SecurityManager.CurrentUserContext.IsSupplyChainUser())
        //            throw new Exception(VBSConstants.IlegalAccess);

        //        bool canUpdate = await SecurityManager.HasAccessAsync(ResourceIds.VBS_BaseInfo_Personel_ChangeStatus);
        //        if (!canUpdate)
        //            throw new OperationAccessException(ResourceIds.VBS_BaseInfo_Personel_ChangeStatus);

        //        Personel personnel = await Rule.FindAsync(personnelId);
        //        if (personnel == null)
        //            throw new BPJValidationException("کارمندی یافت نشد");

        //        personnel.DarHalKhedmat = !personnel.DarHalKhedmat;

        //        OperationAccess oprAccess = this.BusinessRule.OperationAccess.Clone() as OperationAccess;
        //        oprAccess.CanUpdate = canUpdate;
        //        Rule.OperationAccess = oprAccess;

        //        VBSUserInfoRule userInfoRule = new VBSUserInfoRule(Rule.OperationAccess, Rule.UnitOfWork);
        //        VBSUserInfo personnelUser = await userInfoRule.Queryable().SingleOrDefaultAsync(user => user.PersonnelID == personnelId);

        //        Rule.Update(personnel);
        //        if (personnelUser != null)
        //        {
        //            personnelUser.IsActive = personnel.DarHalKhedmat;
        //            personnelUser.NeedOTP = true;
        //            personnelUser.OTPCode = null;
        //            personnelUser.LastOTPDate = null;
        //            personnelUser.OTPTryNo = null;
        //            userInfoRule.Update(personnelUser);
        //        }
        //        await Rule.SaveChangesAsync();

        //        return Request.CreateResponse(HttpStatusCode.OK, new { resultCode = (int)ResultCode.Successful, data = personnelId });
        //    }
        //    catch (Exception ex)
        //    {
        //        if (!(ex is BPJValidationException || ex is OperationAccessException || ex is UserContextNullException))
        //        {
        //            var newEx = new Exception($"PersonnelID: {personnelId}, Message: {ex.Message}", ex); //when assign innerException with exception or ex, StackTrace and other data is set;
        //            return await this.HandleExceptionAsync(newEx);
        //        }
        //        else
        //            return await this.HandleExceptionAsync(ex);
        //    }
        //}

        //public async Task<HttpResponseMessage> GetChangeStatusOperationAccess()
        //{
        //    try
        //    {
        //        SecurityManager.ThrowIfUserContextNull();
        //        OperationAccess operationAccess = new OperationAccess { CanUpdate = await SecurityManager.HasAccessAsync(ResourceIds.VBS_BaseInfo_Personel_ChangeStatus) };
        //        return Request.CreateResponse(HttpStatusCode.OK, new { resultCode = (int)ResultCode.Successful, data = operationAccess });
        //    }
        //    catch (Exception ex)
        //    {
        //        return await this.HandleExceptionAsync(ex);
        //    }
        //}

        public async Task <HttpResponseMessage> SavePersonel(PersonelVM personelVM)
        {
            try
            {
                SecurityManager.ThrowIfUserContextNull();
                //if (!SecurityManager.CurrentUserContext.IsOrganizationPersonnel() && !SecurityManager.CurrentUserContext.IsDeveloperUser() &&
                //    !SecurityManager.CurrentUserContext.IsAdminUser() && !SecurityManager.CurrentUserContext.IsSupplyChainUser())
                //    throw new Exception(VBSConstants.IlegalAccess);

                var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId);

                Personel personel = TranslateEntityVMToEntity(personelVM);

                //ShakhsRule shakhsRule = new ShakhsRule(Rule.OperationAccess, Rule.UnitOfWork);
                //int personelShakhsId = await shakhsRule.Queryable()
                //                                       .Where(sh => sh.CodeMeli == personel.Shakhs.CodeMeli)
                //                                       .Select(sh => sh.ID)
                //                                       .SingleOrDefaultAsync();

                //if (personelShakhsId > 0)
                //    personel.Shakhs.ID = personelShakhsId;

                //if (personel.Shakhs.ID > 0)
                //    personel.Shakhs.ObjectState = ObjectState.Modified;
                //else
                //    personel.Shakhs.ObjectState = ObjectState.Added;

                //چون کاربران سازمانی دسترسی اضافه نمودن کارمند دارند ولی دسترسی ویرایش کارمند ندارند،
                //این کد باعث می شود در صورتیکه کارمند اضافه گردد ولی شخص آن به روز رسانی گردد ایجاد مشکل محدویت دسترسی نداشته باشد
                //if (personel.ID <= 0 && personel.Shakhs.ID > 0)
                //{
                //    OperationAccess oprAccess = Rule.OperationAccess.Clone() as OperationAccess;
                //    oprAccess.CanUpdate = Rule.OperationAccess.CanInsert;
                //    Rule.OperationAccess = oprAccess;
                //    shakhsRule.OperationAccess = oprAccess;
                //}

                //shakhsRule.InsertOrUpdateGraph(personel.Shakhs);
                //await shakhsRule.SaveChangesAsync();

                //personel.ShakhsId = personel.Shakhs.ID;

                personel.AzTarikh       = DateTime.Now;// Utilities.PersianDateUtils.ToDateTime(personelVM.DisplayAzTarikh);
                personel.OrganizationId = organId;

                #region UserInfo
                ZhivarUserInfoRule userInfoRule = new ZhivarUserInfoRule(Rule.OperationAccess, Rule.UnitOfWork);
                UserInfo           userInfo     = await userInfoRule.Queryable()
                                                  .Where(user => user.ID == personel.UserID)
                                                  .SingleOrDefaultAsync();

                int oldPositionId = 0;
                if (personel.ID > 0)
                {
                    //Delete Old Position
                    oldPositionId = await Rule.Queryable()
                                    .Where(p => p.ID == personel.ID)
                                    .Select(p => p.RoleID)
                                    .SingleOrDefaultAsync();
                }

                if (userInfo != null)
                {
                    //if (personelVM.NeedToSaveUser)
                    // {
                    //userInfo.PersonnelID = personel.ID;
                    userInfo.OrganizationId     = organId;
                    userInfo.FirstName          = personelVM.FirstName;
                    userInfo.LastName           = personelVM.LastName;
                    userInfo.MobileNo           = personelVM.MobileNo;
                    userInfo.NationalCode       = personelVM.NationalCode;
                    userInfo.Gender             = personel.User.Gender;
                    userInfo.Email              = personel.User.Email;
                    userInfo.Tel                = personel.User.Tel;
                    userInfo.UserName           = personelVM.UserName;
                    userInfo.Tag5               = personelVM.NeedToSign.ToString();
                    userInfo.IsActive           = personel.DarHalKhedmat;
                    userInfo.NeedOTP            = true;
                    userInfo.OTPCode            = null;
                    userInfo.LastOTPDate        = null;
                    userInfo.OTPTryNo           = null;
                    userInfo.IsDeleted          = false;
                    userInfo.AuthenticationType = 3;

                    await userInfoRule.LoadCollectionAsync(userInfo, u => u.UserRoles);

                    if (userInfo.UserRoles == null)
                    {
                        userInfo.UserRoles = new List <UserRole>();
                    }

                    if (oldPositionId != 0 && oldPositionId != personel.RoleID)
                    {
                        foreach (var userRole in userInfo.UserRoles.Where(ur => ur.RoleId == oldPositionId))
                        {
                            userRole.ObjectState = ObjectState.Deleted;
                        }
                    }

                    if (!userInfo.UserRoles.Any(ur => ur.RoleId == personel.RoleID))
                    {
                        userInfo.UserRoles.Add(new UserRole()
                        {
                            ObjectState = ObjectState.Added, RoleId = personel.RoleID, UserId = userInfo.ID
                        });
                    }
                    //}
                    //else
                    //{
                    //    userInfo.NeedOTP = true;
                    //    userInfo.OTPCode = null;
                    //    userInfo.LastOTPDate = null;
                    //    userInfo.OTPTryNo = null;
                    //    userInfo.IsActive = false;
                    //    userInfo.IsDeleted = true;
                    //}

                    userInfo.ObjectState = ObjectState.Modified;
                    userInfoRule.InsertOrUpdateGraph(userInfo);
                    await Rule.SaveChangesAsync();
                }
                else
                {
                    userInfo = new UserInfo()
                    {
                        ApplicationId = ConfigurationController.ApplicationID,
                        ObjectState   = ObjectState.Added,
                        //PersonnelID = personel.ID,
                        OrganizationId = personel.OrganizationId,
                        //Tag1 = personel.ID.ToString(),
                        FirstName          = personelVM.FirstName,
                        LastName           = personelVM.LastName,
                        MobileNo           = personelVM.MobileNo,
                        NationalCode       = personelVM.NationalCode,
                        Gender             = personelVM.Gender,
                        AuthenticationType = (int)ZhivarEnums.ZhivarUserType.Organization,
                        Email         = personelVM.Email,
                        Password      = CryptoHelper.ComputeHash(personelVM.Password),
                        PlainPassword = personelVM.Password,
                        Tel           = personelVM.Tel,
                        UserName      = personelVM.UserName,
                        NeedOTP       = false,
                        OTPCode       = null,
                        LastOTPDate   = null,
                        OTPTryNo      = null,
                        //Tag5 = personelVM.NeedToSign.ToString(),
                        IsActive     = true,
                        IsDeleted    = false,
                        LoginTryTime = 0,
                    };

                    userInfo.UserRoles = new List <UserRole>()
                    {
                        new UserRole()
                        {
                            ObjectState = ObjectState.Added, RoleId = personel.RoleID
                        }
                    };

                    userInfoRule.InsertOrUpdateGraph(userInfo);
                    await Rule.SaveChangesAsync();
                }
                #endregion

                personel.UserID = userInfo.ID;

                if (personel.ID > 0)
                {
                    Rule.Update(personel);
                }
                else
                {
                    Rule.Insert(personel);
                }

                await Rule.SaveChangesAsync();

                return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = (int)ResultCode.Successful, data = personel.ID }));
            }
            catch (Exception ex)
            {
                return(await this.HandleExceptionAsync(ex));
            }
        }