Example #1
0
        public virtual Result Update(T entity, BaseAppUser user, Action <bool, T> action = null)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (entity.Id <= 0)
            {
                throw new ArgumentException("实体ID无效,必须大于0", nameof(entity));
            }
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user), "可跟踪实体必须提供操作人信息");
            }

            entity.BeforeUpdate(user);

            var error = BeforeUpdate(entity, user);

            if (!string.IsNullOrWhiteSpace(error))
            {
                return(ResultUtil.AuthFail(error));
            }

            var row     = Db.Update(entity);
            var success = row > 0;

            action?.Invoke(success, entity);
            return(success ? ResultUtil.Success() : ResultUtil.Fail());
        }
Example #2
0
 public int Remove <T>(int[] ids, BaseAppUser user) where T : TraceEntity
 {
     using (var conn = GetConnection())
     {
         return(conn.Remove <T>(ids, user));
     }
 }
Example #3
0
 public int Remove(string table, int[] ids, BaseAppUser user)
 {
     using (var conn = GetConnection())
     {
         return(conn.Remove(table, ids, user));
     }
 }
Example #4
0
        public static int Remove <T>(this SqlConnection connection, BaseAppUser user, MySearchUtil util, SqlTransaction trans = null)
            where T : BaseEntity
        {
            var table = MyContainer.Get(typeof(T)).Table;

            return(connection.Remove(table, user, util, trans));
        }
Example #5
0
 public int Remove <T>(List <T> entities, BaseAppUser user) where T : TraceEntity
 {
     using (var conn = GetConnection())
     {
         return(conn.Remove(entities, user));
     }
 }
Example #6
0
 public int Remove(string table, MySearchUtil util, BaseAppUser user)
 {
     using (var conn = GetConnection())
     {
         return(conn.Remove(table, user, util));
     }
 }
Example #7
0
        public static int Remove <T>(this SqlConnection connection, int[] ids, BaseAppUser user, SqlTransaction trans = null)
            where T : TraceEntity
        {
            var table = MyContainer.Get(typeof(T)).Table;

            return(connection.Remove(table, ids, user, trans));
        }
        public Result Update(RoleEditDto dto, BaseAppUser user)
        {
            if (dto == null)
            {
                throw new ArgumentNullException(nameof(dto));
            }
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var error = BeforeUpdate(dto);

            if (!string.IsNullOrWhiteSpace(error))
            {
                return(ResultUtil.Fail(error));
            }

            var emptyArrayJson = JsonConvert.SerializeObject(new int[] { });

            var entity = new RoleEntity
            {
                Id   = dto.Id,
                Name = dto.Name,
                DataPermissionType = dto.DataPermissionType,
                DepartmentIdJson   = JsonConvert.SerializeObject(dto.Departments),
            };

            entity.BeforeCreate(user);

            using (var conn = _db.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        var row = conn.Update(entity);
                        conn.Delete <RolePowerEntity>(MySearchUtil.New().AndEqual("RoleId", dto.Id));
                        if (dto.Powers.Any())
                        {
                            conn.Create(dto.Powers
                                        .Select(p => new RolePowerEntity {
                                RoleId = dto.Id, PowerId = p, ColumnCodeJson = emptyArrayJson
                            })
                                        .ToList());
                        }
                        trans.Commit();
                        return(row > 0 ? ResultUtil.Success() : ResultUtil.Fail());
                    }
                    catch (Exception e)
                    {
                        trans.Rollback();
                        conn.Close();
                        return(ResultUtil.Fail(e.Message));
                    }
                }
            }
        }
        public Result Create(RoleEditDto dto, BaseAppUser user)
        {
            if (dto == null)
            {
                throw new ArgumentNullException(nameof(dto));
            }
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var error = BeforeCrete(dto);

            if (!string.IsNullOrWhiteSpace(error))
            {
                return(ResultUtil.Fail(error));
            }

            var entity = new RoleEntity
            {
                Name = dto.Name,
                DataPermissionType = dto.DataPermissionType,
                DepartmentIdJson   = JsonConvert.SerializeObject(dto.Departments),
            };

            entity.BeforeCreate(user);

            using (var conn = _db.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        var id = conn.Create(entity);
                        if (dto.Powers.Any())
                        {
                            conn.Create(dto.Powers
                                        .Select(p => new RolePowerEntity {
                                RoleId = id, PowerId = p
                            })
                                        .ToList());
                        }
                        trans.Commit();
                        return(id > 0 ? ResultUtil.Success(id) : ResultUtil.Fail());
                    }
                    catch (Exception e)
                    {
                        trans.Rollback();
                        conn.Close();
                        return(ResultUtil.Fail(e.Message));
                    }
                }
            }
        }
        public async Task <IHttpActionResult> GetExternalLogin(string provider, string error = null)
        {
            if (error != null)
            {
                return(Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error)));
            }

            if (!User.Identity.IsAuthenticated)
            {
                return(new ChallengeResult(provider, this));
            }

            ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

            if (externalLogin == null)
            {
                return(InternalServerError());
            }

            if (externalLogin.LoginProvider != provider)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                return(new ChallengeResult(provider, this));
            }

            BaseAppUser user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider,
                                                                             externalLogin.ProviderKey));

            bool hasRegistered = user != null;

            if (hasRegistered)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

                ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync <TUserManager, TUser>(UserManager,
                                                                                                          OAuthDefaults.AuthenticationType);

                ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync <TUserManager, TUser>(UserManager,
                                                                                                           CookieAuthenticationDefaults.AuthenticationType);

                AuthenticationProperties properties = ApplicationOAuthProvider <TUserManager, TUser> .CreateProperties(user.UserName);

                Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);
            }
            else
            {
                IEnumerable <Claim> claims   = externalLogin.GetClaims();
                ClaimsIdentity      identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
                Authentication.SignIn(identity);
            }

            return(Ok());
        }
Example #11
0
        public static int Remove(this SqlConnection connection, string table, BaseAppUser user,
                                 MySearchUtil util, SqlTransaction trans = null)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (util == null)
            {
                throw new ArgumentNullException(nameof(util));
            }

            var where = util.GetWhere();
            var param = util.GetParam();

            param.Add("Updator", user.Name);
            var sql = $"UPDATE [{table}] SET IsDel=1,UpdateAt=GETDATE(),Updator=@Updator WHERE {where}";

            return(connection.Execute(sql, param, trans));
        }
Example #12
0
        public override string BeforeUpdate(DepartmentEntity entity, BaseAppUser user)
        {
            var count = Db.Count <DepartmentEntity>(MySearchUtil.New()
                                                    .AndEqual("ParentId", entity.Id)
                                                    .AndEqual("IsDel", false));

            if (count > 0)
            {
                return("此部门下存在有效子部门,禁止删除");
            }

            // 还要验证部门下是否存在有效员工
            count = Db.Count <UserEntity>(MySearchUtil.New()
                                          .AndEqual("IsDel", false)
                                          .And($"Id IN SELECT UserId FROM Base_DepartmentUser WHERE DepartmentId={entity.Id}"));
            if (count > 0)
            {
                return("此部门下存在有效员工,禁止删除");
            }
            return(string.Empty);
        }
        public Result Remove(int id, BaseAppUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var entity = _db.Load <RoleEntity>(id);

            if (entity == null)
            {
                return(ResultUtil.AuthFail("请求的数据不存在"));
            }

            if (entity.IsDel)
            {
                return(ResultUtil.Success());
            }

            using (var conn = _db.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        var row = conn.Remove(entity, user);
                        conn.Delete <RolePowerEntity>(MySearchUtil.New().AndEqual("RoleId", entity.Id));
                        trans.Commit();
                        return(row > 0 ? ResultUtil.Success() : ResultUtil.Fail());
                    }
                    catch (Exception e)
                    {
                        trans.Rollback();
                        conn.Close();
                        return(ResultUtil.Fail(e.Message));
                    }
                }
            }
        }
Example #14
0
        public virtual Result Remove(T entity, BaseAppUser user, Action <bool, T> action = null)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user), "可跟踪实体必须提供操作人信息");
            }

            var error = BeforeRemove(entity, user);

            if (!string.IsNullOrWhiteSpace(error))
            {
                return(ResultUtil.AuthFail(error));
            }

            var row = Db.Remove(entity, user);

            action?.Invoke(row > 0, entity);
            return(row > 0 ? ResultUtil.Success() : ResultUtil.Fail());
        }
Example #15
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var userManager = context.OwinContext.GetUserManager <TUserManager>();

            BaseAppUser user = await userManager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }

            ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync <TUserManager, TUser>(userManager,
                                                                                                      OAuthDefaults.AuthenticationType);

            ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync <TUserManager, TUser>(userManager,
                                                                                                        CookieAuthenticationDefaults.AuthenticationType);

            AuthenticationProperties properties = CreateProperties(user.UserName);
            AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }
Example #16
0
 public virtual string BeforeRemove(T entity, BaseAppUser user)
 {
     return(string.Empty);
 }
Example #17
0
 public override string BeforeRemove(DepartmentEntity entity, BaseAppUser user)
 {
     return(string.IsNullOrWhiteSpace(entity.Name) ? "部门名称不能为空" : string.Empty);
 }
Example #18
0
        public static int Remove <T>(this SqlConnection connection, IEnumerable <T> entities, BaseAppUser user, SqlTransaction trans = null)
            where T : TraceEntity
        {
            var table = MyContainer.Get(typeof(T)).Table;

            return(connection.Remove(table, entities.Select(e => e.Id).ToArray(), user, trans));
        }
Example #19
0
 public void BeforeUpdate(BaseAppUser user)
 {
     Updator = user.Name;
     UpdateAt = DateTime.Now;
 }
Example #20
0
 public void BeforeCreate(BaseAppUser user)
 {
     Creator = user.Name;
     Updator = user.Name;
 }
Example #21
0
        public static int Remove(this SqlConnection connection, string table, int[] ids, BaseAppUser user, SqlTransaction trans = null)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var sql = $"UPDATE [{table}] SET IsDel=1,UpdateAt=GETDATE(),Updator=@Updator WHERE Id IN @Ids";

            return(connection.Execute(sql, new { Ids = ids, Updator = user.Name }));
        }