Ejemplo n.º 1
0
        public async Task <ContentResult> WebSubscribe([Required, FromBody] Subscription subscription)
        {
            var user = await _userMgr.GetUserAsync(User);

            string ret;

            try
            {
                user.WebSubscription = subscription.ToString();
                await _groupsDb.SaveChangesAsync();

                ret = "OK";
            }
            catch (Exception e)
            {
                ret = e.Message;
            }
            return(Content(ret, "text/plain"));
        }
Ejemplo n.º 2
0
        public async Task SignOut()
        {
            var user = await _usrMgr.GetUserAsync(Context.User);

            try
            {
                await Clients.OthersInGroup(user.InGroupId).SendAsync("signed_out", user.UserName);

                await Groups.RemoveFromGroupAsync(user.ConnectionId, user.InGroupId);

                user.InGroupId       = null;
                user.InGroupPassword = null;
                await _groupDb.SaveChangesAsync();
            }
            catch (Exception e)
            {
                throw new HubException(e.Message);
            }
        }
Ejemplo n.º 3
0
        public async Task <ContentResult> Register([FromBody] GroupRegRequest request)
        {
            var user = await _userMgr.GetUserAsync(User);

            string ret;

            try
            {
                if (user.Group != null)
                {
                    ret = "has_group";
                }
                else if ((await _signMgr.CheckPasswordSignInAsync(user, request.Password, false)).Succeeded != true)
                {
                    ret = "wrong_password";
                }
                else if (!StaticData.IsNameValid(request.GroupName))
                {
                    ret = "invalid_name";
                }
                else if (_groupsDb.Users.Any(c => c.Group == request.GroupName))
                {
                    ret = "name_taken";
                }
                else
                {
                    user.Group            = request.GroupName;
                    user.GroupPassword    = request.GroupPassword;
                    user.GroupLastCleaned = DateTime.UtcNow.Ticks;
                    await _groupsDb.SaveChangesAsync();

                    ret = "OK";
                }
            }
            catch (Exception e)
            {
                ret = e.Message;
            }
            return(Content(ret, "text/plain"));
        }
Ejemplo n.º 4
0
        public async Task <ContentResult> SignOut()
        {
            var user = await _userMgr.GetUserAsync(User);

            string ret;

            try
            {
                await _signMgr.SignOutAsync();

                user.InGroupId       = null;
                user.InGroupPassword = null;
                await _groupsDb.SaveChangesAsync();

                ret = "OK";
            }
            catch (Exception e)
            {
                ret = e.Message;
            }
            return(Content(ret, "text/plain"));
        }
Ejemplo n.º 5
0
        public async Task <ContentResult> RequestRegister([FromBody] RegRequest request)
        {
            string ret;

            if (User.Identity.IsAuthenticated)
            {
                ret = "already_registered";
            }
            else
            {
                try
                {
                    request.Email = request.Email.ToLower();
                    if ((await _userMgr.FindByEmailAsync(request.Email)) != null)
                    {
                        ret = "email_is_taken";
                    }
                    else if (!StaticData.IsNameValid(request.Name))
                    {
                        ret = "invalid_name";
                    }
                    else if ((await _userMgr.FindByNameAsync(request.Name)) != null)
                    {
                        ret = "name_is_taken";
                    }
                    else
                    {
                        Random rand = new Random();
                        int    code;
                        do
                        {
                            code = rand.Next(1234, 9876);
                        }while (_groupsDb.RegRequests.FirstOrDefault(r => r.Code == code) != null);
                        _groupsDb.RegRequests.Add(new RegData
                        {
                            Code        = code,
                            RequestTime = DateTime.UtcNow.Ticks,
                            Name        = request.Name,
                            Email       = request.Email,
                            Password    = request.Password
                        });
                        await _groupsDb.SaveChangesAsync();
                        await CleanInactiveUsers();

                        var mret = await SendMail(request.Email, request.Name, code.ToString());

                        if (mret != "ok")
                        {
                            ret = mret;
                        }
                        else
                        {
                            ret = "pending_" + request.Email;
                        }
                    }
                }
                catch (Exception e)
                {
                    ret = e.Message;
                }
            }
            return(Content(ret, "text/plain"));
        }