Beispiel #1
0
        public static async Task <dynamic> ChangeMemberStatus(string adminId, string uid, string status)
        {
            UserAppMemberSet s = new UserAppMemberSet();

            if (!(from d in s.MemberStatusValues where d == status select d).Any())
            {
                return new { ok = false, msg = string.Format(ResourceUtils.GetString("0b8472f8e1a556b4c90b516e2df1917b", "Status '{0}' is not known."), status) }
            }
            ;
            CallContext cctx = Cntx;

            try
            {
                UserServiceProxy usvc = new UserServiceProxy();

                UserSet us    = new UserSet();
                var     admin = await usvc.LoadEntityByKeyAsync(cctx, adminId);

                if (admin.ID == uid)
                {
                    return new { ok = false, msg = ResourceUtils.GetString("0bdf4ebe91cd037e986f8260069292be", "You shouldn't lock yourself out.") }
                }
                ;
                User u = await usvc.LoadEntityByKeyAsync(cctx, uid);

                if (u.Status != us.StatusValues[0])
                {
                    return new { ok = false, msg = ResourceUtils.GetString("b13fb15f7b82c3438ee9e09ae6a5ba2a", "The user is locked globally. It can not be changed in a particular application.") }
                }
                ;
                var maxadmp = await GetMaxPriority(adminId);

                var maxup = await GetMaxPriority(uid);

                if (maxadmp.Major < maxup.Major || maxadmp.Major == maxup.Major && maxadmp.Minor < maxup.Minor)
                {
                    return new { ok = false, msg = string.Format(ResourceUtils.GetString("0452f93e5e52c7eae26c4fac7aa2d5d7", "Denined! Your role level: {0} is less than the requested one."), maxadmp.Major.ToString() + "/" + maxadmp.Minor), newpwd = "" }
                }
                ;
                UserAppMemberServiceProxy umsrv = new UserAppMemberServiceProxy();
                UserAppMember             um    = await umsrv.LoadEntityByKeyAsync(cctx, ApplicationContext.App.ID, uid);

                if (um == null)
                {
                    return new { ok = false, msg = ResourceUtils.GetString("65318cf0e6b4b76ee9ec91f92405cbb8", "Member not found!") }
                }
                ;
                um.MemberStatus     = status;
                um.LastStatusChange = DateTime.UtcNow;
                await umsrv.AddOrUpdateEntitiesAsync(cctx, s, new UserAppMember[] { um });

                return(new { ok = true, msg = "" });
            }
            catch (Exception e)
            {
                return(new { ok = false, msg = string.Format(ResourceUtils.GetString("49dfe380301a10e682f1b3bc09136542", "Exception: {0}"), e.Message) });
            }
        }
Beispiel #2
0
        public override async Task <TUser> FindAsync(string userName, string password)
        {
            if (HttpContext.Current != null)
            {
                string cacheKey = "userLoginState:" + userName;
                var    error    = HttpContext.Current.Cache[cacheKey] as AuthFailedEventArg;
                if (error != null)
                {
                    ErrorsHandler(userName, error);
                    return(null);
                }
            }
            CallContext      cctx = _cctx.CreateCopy();
            UserServiceProxy usvc = new UserServiceProxy();
            UserSet          us   = new UserSet();
            var lu = await usvc.LoadEntityByNatureAsync(cctx, userName);

            if (lu == null || lu.Count == 0)
            {
                var err = new AuthFailedEventArg
                {
                    FailType    = AuthFailedTypes.UnknownUser,
                    FailMessage = ResourceUtils.GetString("3488820581565e9098c46152335ebb24", "Your don't have an account in the present system, please register!")
                };
                ErrorsHandler(userName, err);
                return(null);
            }
            var u = lu[0];

            if (!u.IsApproved)
            {
                var err = new AuthFailedEventArg
                {
                    FailType    = AuthFailedTypes.ApprovalNeeded,
                    FailMessage = ResourceUtils.GetString("3bdf31486d76404d69c73b90c790f9be", "Your account is pending for approval, please wait!")
                };
                ErrorsHandler(userName, err);
                return(null);
            }
            if (u.Status != us.StatusValues[0])
            {
                var err = new AuthFailedEventArg
                {
                    FailType    = AuthFailedTypes.UserAccountBlocked,
                    FailMessage = string.Format(ResourceUtils.GetString("0bcd70b0b005df9491a0623280ee1f4d", "Your account is in the state of being [{0}], please contact an administrator!"), u.Status)
                };
                ErrorsHandler(userName, err);
                return(null);
            }
            UserAppMemberSet          membs = new UserAppMemberSet();
            UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
            var memb = await mbsvc.LoadEntityByKeyAsync(cctx, app.ID, u.ID);

            if (memb == null)
            {
                var err = new AuthFailedEventArg
                {
                    FailType    = AuthFailedTypes.MemberNotFound,
                    FailMessage = string.Format(ResourceUtils.GetString("d084974602e8940a962aad7d00bf7b3e", "You are not currently a member of \"{0}\", please register."), string.IsNullOrEmpty(app.DisplayName) ? app.Name : app.DisplayName)
                };
                ErrorsHandler(userName, err);
                return(null);
            }
            if (memb.MemberStatus != membs.MemberStatusValues[0])
            {
                if (memb.MemberStatus != membs.MemberStatusValues[3])
                {
                    var err = new AuthFailedEventArg
                    {
                        FailType    = AuthFailedTypes.MembershipBlocked,
                        FailMessage = string.Format(ResourceUtils.GetString("3508707fb8263c95b4c022dd0468235b", "Your membership in \"{0}\" is in the state of being [{1}], please contact an administrator!"), string.IsNullOrEmpty(app.DisplayName) ? app.Name : app.DisplayName, memb.MemberStatus)
                    };
                    ErrorsHandler(userName, err);
                    return(null);
                }
                else
                {
                    var      windowStart = u.FailedPasswordAttemptWindowStart.HasValue ? u.FailedPasswordAttemptWindowStart.Value : DateTime.MinValue;
                    DateTime windowEnd   = windowStart.AddSeconds((Store as UserStore <TUser>).PasswordAttemptWindow);
                    if (DateTime.UtcNow <= windowEnd)
                    {
                        var err = new AuthFailedEventArg
                        {
                            FailType    = AuthFailedTypes.MembershipFrozen,
                            FailMessage = string.Format(ResourceUtils.GetString("99529364b5dfda1d15a5859cd49c5a7c", "Maximum login attemps for \"{0}\" exceeded, please try again later!"), string.IsNullOrEmpty(app.DisplayName) ? app.Name : app.DisplayName)
                        };
                        ErrorsHandler(userName, err, false);
                        return(null);
                    }
                    else
                    {
                        memb.MemberStatus               = membs.MemberStatusValues[0];
                        memb.IsMemberStatusModified     = true;
                        memb.LastStatusChange           = DateTime.UtcNow;
                        memb.IsLastStatusChangeModified = true;
                        await mbsvc.AddOrUpdateEntitiesAsync(cctx, membs, new UserAppMember[] { memb });

                        var err = new AuthFailedEventArg
                        {
                            FailType    = AuthFailedTypes.MembershipRecovered,
                            FailMessage = ResourceUtils.GetString("8cdaed0e2a0dd2e31c4960412351d4b5", "Your membership status is restored, please try again!")
                        };
                        if (u.FailedPasswordAttemptCount != 0)
                        {
                            u.FailedPasswordAttemptCount = 0;
                            usvc.EnqueueNewOrUpdateEntities(cctx, us, new User[] { u });
                        }
                        ErrorsHandler(userName, err, false);
                        return(null);
                    }
                }
            }
            TUser user = new TUser();

            user.UpdateInstance(u);
            var found = await base.FindAsync(userName, password);

            if (found == null)
            {
                await(Store as UserStore <TUser>).UpdateFailureCountAsync(cctx, user, "password");
                var err = new AuthFailedEventArg
                {
                    FailType    = AuthFailedTypes.InvalidCredential,
                    FailMessage = ResourceUtils.GetString("3a2a06b3a1f05cde765219211bf2e9be", "Invalid username or password.")
                };
                ErrorsHandler(userName, err, false);
            }
            else
            {
                u.LastLoginDate           = DateTime.UtcNow;
                u.IsLastLoginDateModified = true;
                usvc.EnqueueNewOrUpdateEntities(cctx, new UserSet(), new User[] { u });
                memb.LastActivityDate           = u.LastLoginDate;
                memb.IsLastActivityDateModified = true;
                mbsvc.EnqueueNewOrUpdateEntities(cctx, membs, new UserAppMember[] { memb });
            }
            return(found);
        }