Exemplo n.º 1
0
 private User TryCreateUser(SGContext context, LoginToken token, string email, string password)
 {
     try
     {
         var userTask = UserTasks.CreateUserAsync(context, token, email, "User", password, UserRole.User);
         userTask.Wait();
         var user = userTask.Result;
         return(user);
     }
     catch
     {
         return(null);
     }
 }
Exemplo n.º 2
0
        public async Task <User> createUser(Int64 tokenId, string email, string displayName, string password, int role)
        {
            var token = await quickGetToken(tokenId);

            var userRole = new UserRole(token.User.RawRole);

            if (!userRole.IsAdmin)
            {
                throw AutoApiError.Unauthorised();
            }

            if (string.IsNullOrWhiteSpace(email))
            {
                throw AutoApiError.InvalidParam("email");
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                throw AutoApiError.InvalidParam("password");
            }

            if (!UserRole.RoleIsValid(role))
            {
                throw AutoApiError.InvalidParam("role");
            }
            try
            {
                var user = await UserTasks.CreateUserAsync(_context, token, email, displayName, password, role);

                if (user == null)
                {
                    throw AutoApiError.ServerError("Create user failed unexpectedly.");
                }
                return(user.CloneForExport());
            }
            catch (Exception ex)
            {
                if (ex.Message == "Unauthorised")
                {
                    throw AutoApiError.Unauthorised();
                }
                throw;
            }
        }