Exemple #1
0
        public async Task <ActionResult <User> > InsertUser([FromBody] User user, [FromHeader(Name = "Authorization")] string inputToken)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (user != null)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_user"))
                    {
                        using var db = new SSOContext();
                        var getUsers = await(from users in db.Users
                                             where users.Email == user.Email
                                             select users).IgnoreQueryFilters().ToListAsync();
                        if (getUsers.Any())
                        {
                            foreach (var item in getUsers)
                            {
                                if (item.DeletedAt == null)
                                {
                                    throw new ArgumentNullException("Existing User.");
                                }
                            }
                        }
                        user.Password = BCrypt.Net.BCrypt.HashPassword(user.Password, ConfigurationExtensions.GetConfig().Salt.Value);
                        db.Users.Add(user);
                        await db.SaveChangesAsync();

                        db.Entry(user).State = EntityState.Detached;
                        return(await db.Users.Include(z => z.Company)
                               .Where(x => x.Id == user.Id).FirstAsync());
                    }
                }
            }
            throw new ArgumentNullException();
        }
Exemple #2
0
        public async Task <ActionResult <InviteRole> > PutInviteRole([FromBody] InviteRole inviteRole,
                                                                     [FromHeader(Name = "Authorization")] string inputToken,
                                                                     [FromRoute] int id)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (id == inviteRole.Id)
                {
                    if (permissionCheck.IsAllowed(inputToken, "root"))
                    {
                        var oldInviteRole = await _context.InviteRoles.FindAsync(id);

                        if (oldInviteRole == null)
                        {
                            throw new ArgumentNullException();
                        }

                        inviteRole.CreatedAt = oldInviteRole.CreatedAt;
                        _context.Entry(oldInviteRole).State = EntityState.Detached;

                        _context.InviteRoles.Update(inviteRole);
                        await _context.SaveChangesAsync();

                        _context.Entry(inviteRole).State = EntityState.Detached;
                        return(await _context.InviteRoles
                               .Include(x => x.Role)
                               .Include(x => x.Invite)
                               .Where(x => x.Id == id)
                               .FirstAsync());
                    }
                }
            }
            throw new ArgumentNullException();
        }
 public IActionResult SetUserRoles(int id, [FromBody] RolesIdModel rolesIdModel, [FromHeader(Name = "Authorization")] string inputToken)
 {
     if (tokenCheck.TokenChecking(inputToken))
     {
         if (permissionCheck.IsAllowed(inputToken, "set_roles_to_user"))
         {
             if (setUserRoles.AssignUserToRoles(id, rolesIdModel.RolesId))
             {
                 throw new SuccessCustomException();
             }
         }
     }
     throw new ArgumentNullException();
 }
        public async Task <ActionResult <Permission> > InsertPermission([FromBody] Permission permission, [FromHeader(Name = "Authorization")] string inputToken)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (permission != null)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_permissions"))
                    {
                        using var db = new SSOContext();
                        db.Permissions.Add(permission);
                        await db.SaveChangesAsync();

                        db.Entry(permission).State = EntityState.Detached;
                        return(await db.Permissions.FindAsync(permission.Id));
                    }
                }
            }
            throw new ArgumentNullException();
        }
Exemple #5
0
        public async Task <ActionResult <Company> > InsertCompany([FromBody] Company company, [FromHeader(Name = "Authorization")] string inputToken)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (company != null)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_companies"))
                    {
                        using var db = new SSOContext();
                        db.Companies.Add(company);
                        await db.SaveChangesAsync();

                        db.Entry(company).State = EntityState.Detached;
                        return(await db.Companies.FindAsync(company.Id));
                    }
                }
            }
            throw new ArgumentNullException();
        }
Exemple #6
0
        public async Task <ActionResult <Role> > InsertRole([FromBody] Role role, [FromHeader(Name = "Authorization")] string inputToken)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (role != null)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_roles"))
                    {
                        using var db = new SSOContext();
                        db.Roles.Add(role);
                        await db.SaveChangesAsync();

                        db.Entry(role).State = EntityState.Detached;
                        return(await(from roles in db.Roles
                                     .Include(z => z.Company)
                                     where roles.Id == role.Id
                                     select roles).FirstAsync());
                    }
                }
            }
            throw new ArgumentNullException();
        }
Exemple #7
0
        public async Task <ActionResult <CompanyAirports> > PostCompanyAirports([FromBody] CompanyAirports companyAirports, [FromHeader(Name = "Authorization")] string inputToken)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (companyAirports != null)
                {
                    if (permissionCheck.IsAllowed(inputToken, "root"))
                    {
                        _context.CompanyAirportsSet.Add(companyAirports);
                        await _context.SaveChangesAsync();

                        _context.Entry(companyAirports).State = EntityState.Detached;

                        return(await _context.CompanyAirportsSet
                               .Include(x => x.Company)
                               .Where(x => x.Id == companyAirports.Id)
                               .FirstAsync());
                    }
                }
            }
            throw new ArgumentNullException();
        }
        public async Task <ActionResult <UserParams> > InsertUserParams([FromBody] UserParams userParams, [FromHeader(Name = "Authorization")] string inputToken)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (userParams != null)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_user_params"))
                    {
                        using var db = new SSOContext();
                        db.UserParameters.Add(userParams);
                        await db.SaveChangesAsync();

                        db.Entry(userParams).State = EntityState.Detached;
                        return(await db.UserParameters
                               .Include(z => z.User)
                               .ThenInclude(x => x.Company)
                               .Where(x => x.Id == userParams.Id)
                               .FirstAsync());
                    }
                }
            }
            throw new ArgumentNullException();
        }
Exemple #9
0
        public async Task <ActionResult <Invite> > PostInvite([FromBody] Invite invite, [FromHeader(Name = "Authorization")] string inputToken)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (invite != null)
                {
                    if (permissionCheck.IsAllowed(inputToken, "root"))
                    {
                        try
                        {
                            //create query string
                            var    builder = new UriBuilder(config.SignUpURL);
                            var    query   = HttpUtility.ParseQueryString(builder.Query);
                            string token   = BCrypt.Net.BCrypt.HashPassword(invite.Email + DateTimeOffset.UtcNow.ToString(), config.Salt.Value);
                            query.Add("token", token);
                            builder.Query = query.ToString();
                            string url = builder.ToString();//link to the signup
                            invite.Token = token;
                            //send to sns
                            var data = new
                            {
                                invite.Email,
                                Subject = "Notification",
                                Text    = $"This is your invitation link: {url}"
                            };
                            var client         = httpClientFactory.CreateClient();
                            var requestMessage = new HttpRequestMessage(HttpMethod.Post, config.SNS.Url);
                            requestMessage.Headers.Add("Accept", "*/*");
                            requestMessage.Content = new StringContent(JsonConvert.SerializeObject(data), System.Text.Encoding.UTF8, "application/json");
                            //requestMessage.Headers.Add("Bearer", token);
                            var response       = client.SendAsync(requestMessage).Result;
                            var responseString = response.Content.ReadAsStringAsync().Result;
                            var responseStatus = response.StatusCode;
                            //var outputDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseString);
                            //return outputDictionary.Values.First();

                            //create invite
                            if (responseStatus == HttpStatusCode.OK)
                            {
                                _context.Invites.Add(invite);
                                await _context.SaveChangesAsync();

                                _context.Entry(invite).State = EntityState.Detached;

                                return(await _context.Invites
                                       .Include(x => x.Company)
                                       .Where(x => x.Id == invite.Id)
                                       .FirstAsync());

                                //create user and disable token
                            }
                        }
                        catch (Exception e)
                        {
                            throw e;
                        }
                    }
                }
            }
            throw new ArgumentNullException();
        }