Пример #1
0
        public async Task <ActionResult <UserParams> > UpdateUserParams([FromBody] UserParams userParams,
                                                                        [FromHeader(Name = "Authorization")] string inputToken,
                                                                        [FromRoute] int id)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (id == userParams.Id)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_user_params"))
                    {
                        using var db = new SSOContext();
                        var oldParams = await db.UserParameters.FindAsync(id);

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

                        userParams.CreatedAt      = oldParams.CreatedAt;
                        db.Entry(oldParams).State = EntityState.Detached;

                        db.UserParameters.Update(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 == id)
                               .FirstAsync());
                    }
                }
            }
            throw new ArgumentNullException();
        }
Пример #2
0
        public async Task <ActionResult <Permission> > UpdatePermission([FromBody] Permission permission,
                                                                        [FromHeader(Name = "Authorization")] string inputToken,
                                                                        [FromRoute] int id)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (id == permission.Id)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_permissions"))
                    {
                        using var db = new SSOContext();
                        var oldPermission = await db.Permissions.FindAsync(id);

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

                        permission.CreatedAt          = oldPermission.CreatedAt;
                        db.Entry(oldPermission).State = EntityState.Detached;

                        db.Permissions.Update(permission);
                        await db.SaveChangesAsync();

                        db.Entry(permission).State = EntityState.Detached;
                        return(await db.Permissions.FindAsync(permission.Id));
                    }
                }
            }
            throw new ArgumentNullException();
        }
Пример #3
0
        public async Task <ActionResult <Role> > UpdateRole([FromBody] Role role,
                                                            [FromHeader(Name = "Authorization")] string inputToken,
                                                            [FromRoute] int id)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (id == role.Id)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_roles"))
                    {
                        using var db = new SSOContext();
                        var oldRole = await db.Roles.FindAsync(id);

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

                        role.CreatedAt          = oldRole.CreatedAt;
                        db.Entry(oldRole).State = EntityState.Detached;

                        db.Roles.Update(role);
                        await db.SaveChangesAsync();

                        db.Entry(role).State = EntityState.Detached;
                        return(await(from roles in db.Roles
                                     .Include(z => z.Company)
                                     where roles.Id == id
                                     select roles).FirstAsync());
                    }
                }
            }
            throw new ArgumentNullException();
        }
Пример #4
0
        public async Task <ActionResult <Company> > UpdateCompany([FromBody] Company company,
                                                                  [FromHeader(Name = "Authorization")] string inputToken,
                                                                  [FromRoute] int id)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (id == company.Id)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_companies"))
                    {
                        using var db = new SSOContext();
                        var oldCompany = await db.Companies.FindAsync(id);

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

                        company.CreatedAt          = oldCompany.CreatedAt;
                        db.Entry(oldCompany).State = EntityState.Detached;

                        db.Companies.Update(company);
                        await db.SaveChangesAsync();

                        db.Entry(company).State = EntityState.Detached;
                        return(await db.Companies.FindAsync(id));
                    }
                }
            }

            throw new ArgumentNullException();
        }
Пример #5
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();
        }
Пример #6
0
        public async Task <ActionResult <UserParams> > DeleteUserParams(int id, [FromHeader(Name = "Authorization")] string inputToken)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (id != 0)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_user_params"))
                    {
                        using var db = new SSOContext();

                        var getUserParams = await db.UserParameters.FindAsync(id);

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

                        getUserParams.DeletedAt = DateTimeOffset.UtcNow;
                        db.UserParameters.Update(getUserParams);
                        await db.SaveChangesAsync();

                        db.Entry(getUserParams).State = EntityState.Detached;
                        return(await(from u_params in db.UserParameters
                                     .IgnoreQueryFilters()
                                     .Include(z => z.User)
                                     .ThenInclude(x => x.Company)
                                     where u_params.Id == id
                                     select u_params).FirstAsync());
                    }
                }
            }
            throw new ArgumentNullException();
        }
Пример #7
0
        public async Task <ActionResult <Permission> > DeletePermission(int id, [FromHeader(Name = "Authorization")] string inputToken)
        {
            if (tokenCheck.TokenChecking(inputToken))
            {
                if (id != 0)
                {
                    if (permissionCheck.IsAllowed(inputToken, "edit_permissions"))
                    {
                        using var db = new SSOContext();
                        var getPermission = await(from permissions in db.Permissions
                                                  where permissions.Id == id
                                                  select permissions).FirstAsync();
                        if (getPermission == null)
                        {
                            throw new ArgumentNullException();
                        }

                        getPermission.DeletedAt = DateTimeOffset.UtcNow;
                        db.Permissions.Update(getPermission);
                        await db.SaveChangesAsync();

                        db.Entry(getPermission).State = EntityState.Detached;
                        return(await db.Permissions
                               .IgnoreQueryFilters()
                               .Where(x => x.Id == id).FirstAsync());
                    }
                }
            }
            throw new ArgumentNullException();
        }
Пример #8
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();
        }
Пример #9
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();
        }
Пример #10
0
        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();
        }
Пример #11
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();
        }
Пример #12
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();
        }
Пример #13
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();
        }