Exemplo n.º 1
0
        public async Task <IHttpActionResult> UpdateUser(int id, tUser tUser)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != tUser.ID)
            {
                return(BadRequest());
            }
            var user = await db.tUsers.FindAsync(id);

            user.PasswordHash = tUser.PasswordHash;

            if (user.PasswordHash != null)
            {
                PasswordStorage oPassUtil = new PasswordStorage();
                //oPassUtil.Hashstring = ;
                oPassUtil.CreateHash(user.PasswordHash);
                //oPassUtil.VerifyPassword
                user.PasswordHash = oPassUtil.Hashstring;

                tSalt salt = new tSalt {
                    Salt = oPassUtil.Saltstring
                };

                SaltsController sl = new SaltsController();
                user.tSalt = await sl.EditSalt(salt.Id, salt);

                if (user.tSalt != null)
                {
                    user.PHSaltID = tUser.tSalt.Id;
                }
            }
            db.Entry(user).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                if (!tUserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            //return StatusCode(HttpStatusCode.NoContent);
            return(StatusCode(HttpStatusCode.OK));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> RegisterUser(tUser tUser)
        {
            //tUser user = await db.tUsers.FirstAsync(x => x.Email == tUser.Email);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //var user = db.tUsers.FirstOrDefault(x => x.Email == tUser.Email);
            //if (user != null)
            //{
            //    return Conflict();
            //}

            try
            {
                tUser.ExternalID      = Guid.NewGuid();
                tUser.CreateDateTime  = DateTime.Now;
                tUser.AccountStatusID = 1;
                //tUser.PHSaltID = 1;

                if (tUser.PasswordHash != null)
                {
                    PasswordStorage oPassUtil = new PasswordStorage();
                    //oPassUtil.Hashstring = ;
                    oPassUtil.CreateHash(tUser.PasswordHash);
                    //oPassUtil.VerifyPassword
                    tUser.PasswordHash = oPassUtil.Hashstring;

                    tSalt salt = new tSalt();
                    salt.Salt = oPassUtil.Saltstring;

                    SaltsController sl = new SaltsController();
                    tUser.tSalt = await sl.PosttSalt(salt);

                    tUser.PHSaltID = tUser.tSalt.Id;
                }

                db.tUsers.Add(tUser);
                db.SaveChanges();

                //post new userid to other dbs
                UserData.UserIDsController userIds = new UserData.UserIDsController();
                tUserID userID = new tUserID();
                userID.UserID = tUser.ID;

                await userIds.PosttUserID(userID);

                WebApp.UserIDsController webUsers = new WebApp.UserIDsController();
                await webUsers.PosttUserID(userID);

                //Add default consent to new user's share settings
                tUserSHARESetting            userSHARESetting = new tUserSHARESetting();
                tXrefUserSHARESettingsPurpos userXrefSHARE    = new tXrefUserSHARESettingsPurpos();
                userSHARESetting.AllData        = true;
                userSHARESetting.UserID         = tUser.ID;
                userSHARESetting.SHARESettingID = 1; //allow
                userSHARESetting.SystemStatusID = 1; //valid
                userXrefSHARE.SHARESettingID    = userSHARESetting.ID;
                userXrefSHARE.SHAREPurposeID    = 2; //Research only, any available
                userSHARESetting.tXrefUserSHARESettingsPurposes.Add(userXrefSHARE);

                UserData.UserSHARESettingsController shareController = new UserData.UserSHARESettingsController();
                await shareController.PosttUserSHARESetting(userSHARESetting);

                //return CreatedAtRoute("UsersAPI", new { id = tUser.ID }, tUser);
                return(Ok(tUser));
            }
            catch (Exception ex)
            {
                //Insert Error Log
                tUsersErrLog userErrorLog = new tUsersErrLog();

                userErrorLog.ErrTypeID   = (int)ErrorLogging.enumErrorType.Application;
                userErrorLog.ErrSourceID = (int)AuditLogging.enumApplication.SFCBAL;
                userErrorLog.Code        = ex.HResult.ToString();
                userErrorLog.Description = ex.Message;
                userErrorLog.Trace       = ex.StackTrace;

                db.tUsersErrLogs.Add(userErrorLog);
                db.SaveChanges();

                string ErrMsg = "An error occured and we have logged the error. Please try again later.";

                Exception Err = new Exception(ErrMsg, ex);

                return(InternalServerError(Err));
            }
        }