コード例 #1
0
        public async Task <ServicesModel.Models.Auth.SendAuth> ReturnAuth(int user_id, string role)
        {
            SendAuth send = null;

            if (role == "owner")
            {
                var account = await _context.Accounts.Where(x => x.id_user == user_id).FirstOrDefaultAsync();

                send = new SendAuth
                {
                    accountid = account.id,
                    isfilled  = CheckFilled(account),
                    name      = account.name,
                    role      = role
                };
            }
            else if (role == "staff")
            {
                var account = await _context.EmployeeOwners.Where(x => x.id_user == user_id).FirstOrDefaultAsync();

                send = new SendAuth
                {
                    accountid = account.id,
                    isfilled  = CheckFilledStaff(account),
                    name      = account.firstname,
                    role      = role
                };
            }
            else
            {
                var account = await _context.Clients.Where(x => x.id_user == user_id).FirstOrDefaultAsync();

                send = new SendAuth
                {
                    accountid = account.id,
                    isfilled  = CheckFilledClient(account),
                    name      = account.name,
                };
            }
            var token = await _context.Tokens.Where(x => x.user_id == user_id).FirstOrDefaultAsync();

            send.token = token.access;
            return(send);
        }
コード例 #2
0
        public async Task <JsonResult> PostAuthClient([FromBody] Register auth)
        {
            if (ModelState.IsValid)
            {
                Token    token = null;
                SendAuth send  = null;
                try
                {
                    var check = CheckEmail(auth.email);

                    ServicesModel.Models.Auth.Auth authtemp = new ServicesModel.Models.Auth.Auth
                    {
                        data_add       = DateTime.Now,
                        last_visit     = DateTime.Now,
                        is_active      = true,
                        role           = auth.role,
                        password       = auth.password,
                        email          = auth.email,
                        EmailConfirmed = false,
                        UserName       = auth.email,
                    };

                    Client client;

                    if (!await CheckAccount(authtemp))
                    {
                        authtemp.is_active  = true;
                        authtemp.last_visit = DateTime.Now;
                        await _context.Auths.AddAsync(authtemp);

                        await _context.SaveChangesAsync();

                        client = new Client
                        {
                            update_date = DateTime.Now,
                            id_user     = authtemp.id,
                            email       = auth.email,
                            status      = "manual"
                        };

                        //await _context.SaveChangesAsync();
                        //    var id = await _context.Auths.Where(x => x.email == authtemp.email && x.Phone == authtemp.Phone).Select(x => x.id).FirstOrDefaultAsync();
                        token = _auth.Generate_Tokens(authtemp.id, auth.role);

                        await _context.Clients.AddAsync(client);

                        await _context.Tokens.AddAsync(token);

                        if (check == "email")
                        {
                            await _auth.Confirm(authtemp.id, authtemp.email);
                        }
                        await _context.SaveChangesAsync();

                        send = new SendAuth
                        {
                            accountid = client.id,
                            token     = token.access
                        };
                    }
                    else
                    {
                        return(new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.Forbidden, null, "Такой email уже зарегистрирован")));
                    }
                }
                catch (Exception ex)
                {
                    return(new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.Conflict, null, "Такой email уже зарегистрирован")));
                }
                return(new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.Created, send, null)));
            }

            return(new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.BadRequest, null, "Неправильные данные")));
        }
コード例 #3
0
        public async Task <JsonResult> PostAuth([FromBody] Register auth)
        {
            if (ModelState.IsValid)
            {
                Token    token = null;
                SendAuth send  = null;
                try
                {
                    var check = CheckEmail(auth.email);

                    ServicesModel.Models.Auth.Auth authtemp = new ServicesModel.Models.Auth.Auth
                    {
                        data_add   = DateTime.Now,
                        last_visit = DateTime.Now,
                        is_active  = true,
                        role       = auth.role,
                        password   = auth.password
                    };
                    ServicesModel.Models.Account.Account account = new ServicesModel.Models.Account.Account
                    {
                        update = DateTime.Now
                    };
                    if (check == "phone")
                    {
                        authtemp.UserName = auth.email;
                        authtemp.Phone    = auth.email;
                        account.phone     = auth.email;
                    }
                    else if (check == "email")
                    {
                        authtemp.email    = auth.email;
                        authtemp.UserName = auth.email;
                        account.email     = auth.email;
                    }
                    else
                    {
                        return(new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.BadRequest, null, "Укажите номер телефона или почты в правильном формате")));
                    }

                    if (!await CheckAccount(authtemp))
                    {
                        authtemp.is_active = true;
                        await _context.Auths.AddAsync(authtemp);

                        //   var result=await _manager.CreateAsync(authtemp, auth.password);
                        //if (result.Succeeded)
                        //{
                        //    var currentUser = await _manager.FindByNameAsync(auth.email);

                        //    var roleresult = _manager.AddToRoleAsync(authtemp, "owner");
                        //}
                        //else
                        //{
                        //    string error = "";
                        //    foreach (var s in result.Errors.ToList())
                        //    {
                        //        error += s.Description + "\n";
                        //    }
                        //    return new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.Conflict, null,
                        //        error));

                        //}
                        await _context.SaveChangesAsync();

                        var id = await _context.Auths.Where(x => x.email == authtemp.email && x.Phone == authtemp.Phone).Select(x => x.id).FirstOrDefaultAsync();

                        token           = _auth.Generate_Tokens(id, auth.role);
                        account.id_user = id;
                        await _context.Accounts.AddAsync(account);

                        await _context.Tokens.AddAsync(token);

                        if (check == "email")
                        {
                            await _auth.Confirm(id, authtemp.email);
                        }
                        await _context.SaveChangesAsync();

                        var accountsend = await _context.Accounts.Where(x => x.id_user == id).FirstOrDefaultAsync();

                        send = new SendAuth
                        {
                            accountid = accountsend.id,
                            token     = token.access,
                            role      = authtemp.role
                        };
                    }
                    else
                    {
                        return(new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.Forbidden, null, "Такой email уже зарегистрирован")));
                    }
                }
                catch (Exception ex)
                {
                    return(new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.Conflict, null, "Такой email уже зарегистрирован")));
                }
                return(new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.Created, send, null)));
            }

            return(new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.BadRequest, null, "Неправильные данные")));
        }