Пример #1
0
        public async Task <Dto.ApiResponse <object> > CreateInitialAdminRole(Dto.Role model)
        {
            var response = new Dto.ApiResponse <object>();

            using (var tx = _roleRepo.Context.Database.BeginTransaction())
            {
                try
                {
                    //Create initial role for the user
                    var roleEntity         = _mapper.Map <Entity.Role>(model);
                    var createRoleResponse = await this.CreateRole(model);

                    if (createRoleResponse.Success)
                    {
                        //assign the role to the user
                        model = createRoleResponse.DataResponse;
                        var assignedRoleToUserResponse = await this.AssignRoleToUser(model);

                        if (assignedRoleToUserResponse.Success)
                        {
                            //assigned all of the feature to the user
                            var appFeatures = _appFeatureRepo.List();
                            foreach (var appFeature in appFeatures)
                            {
                                await this.AssignRoleApplicationFeatureForUser(new Dto.RoleApplicationFeature
                                {
                                    ApplicationFeatureId = appFeature.Id,
                                    RoleId     = model.Id,
                                    FullAccess = true,
                                    CreatedBy  = MtsResource.AdminCreateUpdateBy,
                                    UpdatedBy  = MtsResource.AdminCreateUpdateBy
                                });
                            }

                            tx.Commit();
                        }
                        else
                        {
                            response.Success       = false;
                            response.ErrorMesssage = assignedRoleToUserResponse.ErrorMesssage;
                        }
                    }
                    else
                    {
                        response.Success       = false;
                        response.ErrorMesssage = createRoleResponse.ErrorMesssage;
                    }
                }
                catch (Exception e)
                {
                    response.Success = false;
                    response.ErrorMesssage.Add(e.GetBaseException().Message);
                    tx.Rollback();
                }
            }
            return(response);
        }
Пример #2
0
        public async Task <Dto.ApiResponse <object> > AssignRoleApplicationFeatureForUser(Dto.RoleApplicationFeature model)
        {
            var response              = new Dto.ApiResponse <object>();
            var roleAppEntity         = _mapper.Map <Entity.RoleApplicationFeature>(model);
            var isRoleAlreadyAssigned = _roleAppRepo.List(i => i.RoleId == model.RoleId && i.ApplicationFeatureId == model.ApplicationFeatureId).Any();

            if (isRoleAlreadyAssigned)
            {
                response.Success = false;
                response.ErrorMesssage.Add(MtsResource.RoleAlreadyAssignToFeature);
            }

            if (response.Success)
            {
                await _roleAppRepo.Save(roleAppEntity);
            }
            return(response);
        }
Пример #3
0
        public async Task <Dto.ApiResponse <object> > AssignRoleToUser(Dto.Role model)
        {
            var response = new Dto.ApiResponse <object>();

            var isRoleAlreadyassignedToUser = _userRoleRepo.List(i => i.RoleId == model.Id && i.UserId == model.UserId).Any();

            if (isRoleAlreadyassignedToUser)
            {
                response.Success = false;
                response.ErrorMesssage.Add(MtsResource.RoleAlreadyAssignToUser);
            }

            var userRoleEntity = _mapper.Map <Entity.UserRole>(model);

            userRoleEntity.RoleId = model.Id;

            await _userRoleRepo.Save(userRoleEntity);

            return(response);
        }