Beispiel #1
0
        public static async Task <DataResponse <string> > SignUp(LoginSignupModel model)
        {
            using (var db = BaseService.CreateSampleContext())
            {
                var response = new DataResponse <string>();
                if (string.IsNullOrWhiteSpace(model.Logon) || string.IsNullOrWhiteSpace(model.Password))
                {
                    response.ResponseCode = 400;
                    response.Message      = "Logon and Password can't be empty";
                    return(response);
                }

                var grupo = await db.GetTenantById(model.TenantId);

                if (grupo == null)
                {
                    response.ResponseCode = 400;
                    response.Message      = "Tenant not found.";
                    return(response);
                }

                db.TenantID = model.TenantId;

                var user = await db.GetUserByLogon(model.Logon);

                if (user != null)
                {
                    response.ResponseCode = 400;
                    response.Message      = "Login Name already in use by another User.";
                    return(response);
                }

                user          = new DataEntities.User();
                user.UserID   = Guid.NewGuid();
                user.Logon    = model.Logon;
                user.TenantID = model.TenantId;
                user.RegisterPassword(model.Password);
                var sessao = user.CreateSession();

                await db.Users.AddAsync(user);

                await db.Sessions.AddAsync(sessao);

                await db.SaveChangesAsync();

                response.ResponseCode = 200;
                response.Data         = Convert.ToBase64String(sessao.Token);
                return(response);
            }
        }
        private static void SeedAdminUser(this SampleContext db)
        {
            var usuarioId = Guid.Parse("{30EA0242-4937-4C2D-8BE4-EA9FA4B2A97E}");
            var usuario   = db.Users.Where(x => x.UserID == usuarioId).FirstOrDefault();

            if (usuario == null)
            {
                usuario        = new DataEntities.User();
                usuario.UserID = usuarioId;
                usuario.Logon  = "admin";
                usuario.RegisterPassword("H3ll0@W0rld");

                db.Users.Add(usuario);
            }
        }