public IActionResult DeleteUser([FromBody] Schedule.Models.User user)
        {
            _dataSource.Users.Remove(user);
            _dataSource.CommitTransaction();

            return(Ok());
        }
        public IActionResult AddUser([FromBody] Schedule.Models.User user)
        {
            _dataSource.Users.Add(user);
            _dataSource.CommitTransaction();

            return(Created(Url.RouteUrl(nameof(GetUser), new { user.Id }), user));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Authorize the user which was authenticated with Oauth.
        /// </summary>
        /// <param name="datasource"></param>
        /// <param name="principal"></param>
        public static void AuthorizeOauthUser(this IDataSource datasource, ClaimsPrincipal principal)
        {
            // TODO: Allow a user to have multiple Oauth accounts from different providers that link to the internal user by it's email account(s).
            // TODO: Redirect user to create a User Profile page.
            var email  = principal.GetEmail()?.Value;
            var userId = datasource.Users.Verify(email);

            Schedule.Models.User user;
            if (userId == 0)
            {
                user = new Schedule.Models.User()
                {
                    Key           = Guid.NewGuid(),
                    LastName      = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.Value,
                    FirstName     = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName)?.Value,
                    Email         = email,
                    OauthAccounts = new List <Schedule.Models.OauthAccount>(new[]
                    {
                        new Schedule.Models.OauthAccount()
                        {
                            Key    = principal.GetNameIdentifier()?.Value,
                            Email  = email,
                            Issuer = principal.GetNameIdentifier().Issuer
                        }
                    })
                };
                datasource.Users.Add(user);
                datasource.CommitTransaction();
            }
            else
            {
                user = datasource.Users.Get(userId);
            }

            var identity = principal.Identity as ClaimsIdentity;
            var claims   = new List <Claim>(new[]
            {
                new Claim(ClaimTypes.Name, $"{user.FirstName} {user.LastName}", typeof(string).FullName, "CoEvent"), // TODO: Issuer namespace.
                new Claim("User", $"{user.Id}", typeof(int).FullName, "CoEvent"),
                new Claim("Account", $"{user.DefaultAccountId ?? user.OwnedAccounts.FirstOrDefault()?.Id ?? 0}", typeof(int).FullName, "CoEvent")
            });

            claims.ForEach(c =>
            {
                var claim = identity.Claims.FirstOrDefault(cl => cl.Type == c.Type);
                if (claim != null)
                {
                    identity?.TryRemoveClaim(claim);
                }
            });
            identity?.AddClaims(claims);
        }