Ejemplo n.º 1
0
        public async Task <string> Create(NewAccountInfo newAccountInfo)
        {
            await DatabaseFunctions.InitializeStaticStorage(_db).ConfigureAwait(false);

            if (newAccountInfo is null)
            {
                return("No credentials received");
            }

            newAccountInfo.CredentialsUsername = newAccountInfo.CredentialsUsername.ToLower();
            // checks if the email is legit or not
            if (!EmailServices.VerifyEmail(newAccountInfo.CredentialsUsername))
            {
                return("Invalid email");
            }

            var credentialList = await DatabaseFunctions.GetCredentials(_db, newAccountInfo).ConfigureAwait(false);

            if (credentialList.Count != 0)
            {
                return("Email already exists");
            }

            var pcCredentialPassword = await DatabaseFunctions.CreateNewCredentials(_db, newAccountInfo).ConfigureAwait(false);

            await DatabaseFunctions.CreateNewAdmin(_db, newAccountInfo).ConfigureAwait(false);

            StaticStorageServices.PcMapper.Add(newAccountInfo.CredentialsUsername, new Dictionary <string, DiagnosticData>());

            //send pcCredential password to the new user
            await EmailServices.SendEmail(newAccountInfo.CredentialsUsername, $"Pc Credential Password: {pcCredentialPassword}");

            return("Success");
        }
Ejemplo n.º 2
0
        // commented because after the first run, the first assertion will fail because we will be having
        // the account created already in the database
        // [Fact]
        public async Task TestCreate()
        {
            var msg1 = new NewAccountInfo()
            {
                AdminFirstName      = "Omar",
                AdminLastName       = "Rony",
                CredentialsUsername = "******",
                CredentialsPassword = "******"
            };
            var response1 = await GetPostServices.Post("https://pc-health.azurewebsites.net/Admin/Create",
                                                       JsonSerializer.Serialize(msg1));

            Assert.Equal("Success", response1);
            var response2 = await GetPostServices.Post("https://pc-health.azurewebsites.net/Admin/Create",
                                                       JsonSerializer.Serialize(msg1));

            Assert.Equal("Email already exists", response2);
            var msg2 = new NewAccountInfo()
            {
                AdminFirstName      = "Omar",
                AdminLastName       = "Rony",
                CredentialsUsername = "******",
                CredentialsPassword = "******"
            };
            var response3 = await GetPostServices.Post("https://pc-health.azurewebsites.net/Admin/Create",
                                                       JsonSerializer.Serialize(msg2));

            Assert.Equal("Invalid email", response3);
        }
Ejemplo n.º 3
0
        public static async Task CreateNewAdmin(PcHealthContext dbContext, NewAccountInfo newAccountInfo)
        {
            var newAdmin = new Admin()
            {
                AdminFirstName           = newAccountInfo.AdminFirstName,
                AdminLastName            = newAccountInfo.AdminLastName,
                AdminCredentialsUsername = newAccountInfo.CredentialsUsername
            };
            await dbContext.Admins.AddAsync(newAdmin).ConfigureAwait(false);

            await dbContext.SaveChangesAsync().ConfigureAwait(false);
        }
Ejemplo n.º 4
0
        public static async Task <string> CreateNewCredentials(PcHealthContext dbContext, NewAccountInfo newAccountInfo)
        {
            var(salt, passwordHash) = Services.HashServices.Encrypt(newAccountInfo.CredentialsPassword);
            var pcCredentialsPassword = ModelCreation.GenerateRandomString();
            var newCredential         = new Credential()
            {
                CredentialsUsername  = newAccountInfo.CredentialsUsername,
                CredentialsPassword  = passwordHash,
                CredentialsSalt      = salt,
                PcCredentialPassword = pcCredentialsPassword
            };
            await dbContext.Credentials.AddAsync(newCredential).ConfigureAwait(false);

            StaticStorageServices.AdminMapper.Add(newAccountInfo.CredentialsUsername, pcCredentialsPassword);
            await dbContext.SaveChangesAsync().ConfigureAwait(false);

            return(pcCredentialsPassword);
        }
Ejemplo n.º 5
0
 public static async Task <List <Credential> > GetCredentials(PcHealthContext dbContext, NewAccountInfo newAccountInfo)
 {
     return(await dbContext.Credentials.Where(c => c.CredentialsUsername == newAccountInfo.CredentialsUsername).ToListAsync().ConfigureAwait(false));
 }