Ejemplo n.º 1
0
        public async Task <PagingResult <View_User> > Get(PagingRequest <UserGetRequest> value)
        {
            if (!ModelState.IsValid)
            {
                return(new PagingResult <View_User>()
                {
                    code = (int)BasicControllerEnums.UnprocessableEntity,

                    message = ModelErrors()
                });
            }

            if (string.IsNullOrWhiteSpace(value.orderby))
            {
                value.orderby = "UserID";
            }

            var q = new PagingService <View_User>(db, value, "View_User")
            {
                where = (where, sqlParams) =>
                {
                    where.Add(" ( Tenants LIKE '%\"TenantId\":" + TenantId + "%') ");

                    if (!User.IsInRole(Roles.Administrators))
                    {
                        where.Add("Lineage.IsDescendantOf(hierarchyid::Parse ('" + UserLineage + "')) = 1");
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.email))
                    {
                        where.Add("Email = @Email");
                        sqlParams.Add(new SqlParameter("@Email", value.q.email));
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.name))
                    {
                        where.Add("UserName like @UserName");
                        sqlParams.Add(new SqlParameter("@UserName", "%" + value.q.name + "%"));
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.phoneNumber))
                    {
                        where.Add("PhoneNumber = @PhoneNumber");
                        sqlParams.Add(new SqlParameter("@PhoneNumber", value.q.phoneNumber));
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.roles))
                    {
                        var roleIds = value.q.roles.Split(new string[] { "," },
                                                          StringSplitOptions.RemoveEmptyEntries).ToList();

                        var rolesExpression = roleIds.Select(r => "Roles Like '%\"Id\":" + r + ",%'");

                        where.Add(" ( " + string.Join(" AND ", rolesExpression) + " ) ");
                    }
                }
            };

            var result = await q.ExcuteAsync(propConverter : (prop, val) =>
            {
                switch (prop.Name)
                {
                case "Roles":
                    return(JsonConvert.DeserializeObject <List <View_User_Role> >(val.ToString()));

                case "Claims":
                    return(JsonConvert.DeserializeObject <List <View_User_Claim> >(val.ToString()));

                case "Files":
                    return(JsonConvert.DeserializeObject <List <View_User_File> >(val.ToString()));

                case "Properties":
                    return(JsonConvert.DeserializeObject <List <View_User_Property> >(val.ToString()));

                case "Tenants":
                    return(JsonConvert.DeserializeObject <List <View_User_Tenant> >(val.ToString()));

                default:
                    return(val);
                }
            });

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <PagingResult <View_User> > Get([FromQuery] PagingRequest <UserGetRequest> value)
        {
            if (!ModelState.IsValid)
            {
                return(new PagingResult <View_User>()
                {
                    code = (int)BasicControllerEnums.UnprocessableEntity,

                    message = ModelErrors()
                });
            }

            if (string.IsNullOrWhiteSpace(value.orderby))
            {
                value.orderby = "UserID";
            }

            var q = new PagingService <View_User>(db, value, "View_User")
            {
                Where = (where, sqlParams) =>
                {
                    where.Add(" Tenants LIKE @TenantId");
                    sqlParams.Add(new SqlParameter("@TenantId", $"%TenantId\":{TenantId}%"));

                    if (!User.IsInRole(DefaultRoles.Administrator) && !string.IsNullOrWhiteSpace(UserLineage))
                    {
                        //where.Add("Lineage.IsDescendantOf(hierarchyid::Parse ('" + UserLineage + "')) = 1");
                        where.Add("Lineage LIKE @Lineage");
                        sqlParams.Add(new SqlParameter("@Lineage", "%" + UserLineage));
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.email))
                    {
                        where.Add("Email = @Email");
                        sqlParams.Add(new SqlParameter("@Email", value.q.email));
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.name))
                    {
                        where.Add("UserName like @UserName");
                        sqlParams.Add(new SqlParameter("@UserName", "%" + value.q.name + "%"));
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.phoneNumber))
                    {
                        where.Add("PhoneNumber = @PhoneNumber");
                        sqlParams.Add(new SqlParameter("@PhoneNumber", value.q.phoneNumber));
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.role))
                    {
                        where.Add("Roles LIKE @Role");
                        sqlParams.Add(new SqlParameter("@Role", $"%Name\":\"{value.q.role}%"));
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.providerName))
                    {
                        where.Add("Logins LIKE @LoginProvider");
                        sqlParams.Add(new SqlParameter("@LoginProvider", $"%LoginProvider\":\"{value.q.providerName}%"));
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.providerKey))
                    {
                        where.Add("Logins LIKE @ProviderKey");
                        sqlParams.Add(new SqlParameter("@ProviderKey", $"%ProviderKey\":\"{value.q.providerKey}%"));
                    }

                    if (!string.IsNullOrWhiteSpace(value.q.claimType) && !string.IsNullOrWhiteSpace(value.q.claimValue))
                    {
                        where.Add("Claims LIKE @ClaimType AND Claims LIKE @ClaimValue");
                        sqlParams.Add(new SqlParameter("@ClaimType", $"%ClaimType\":\"{value.q.claimType}%"));
                        sqlParams.Add(new SqlParameter("@ClaimValue", $"%ClaimValue\":\"{value.q.claimValue}%"));
                    }
                }
            };

            var result = await q.ExcuteAsync(propConverter : (prop, val) =>
            {
                switch (prop.Name)
                {
                case "Roles":
                    return(JsonConvert.DeserializeObject <List <View_User_Role> >(val.ToString()));

                case "Claims":
                    return(JsonConvert.DeserializeObject <List <View_User_Claim> >(val.ToString()));

                case "Files":
                    return(JsonConvert.DeserializeObject <List <View_User_File> >(val.ToString()));

                case "Properties":
                    return(JsonConvert.DeserializeObject <List <View_User_Property> >(val.ToString()));

                case "Tenants":
                    return(JsonConvert.DeserializeObject <List <View_User_Tenant> >(val.ToString()));

                case "Logins":
                    return(JsonConvert.DeserializeObject <List <View_User_Login> >(val.ToString()));

                default:
                    return(val);
                }
            });

            return(result);
        }