Exemplo n.º 1
0
 /// <summary>
 /// Compose access token for user
 /// </summary>
 /// <param name="user"></param>
 /// <returns></returns>
 /// <exception cref="AppsInvalidUserException"></exception>
 internal string ComposeToken(user user)
 {
     var expiration = DateTimeOffset.Now + TimeSpan.FromDays(2);
     var access = GetAccessType(user.role_id);
     var token = Token.CreateAndSign(user.username, user.image, access, expiration);
     return token.Signature;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Save updated user or new created user to the database
        /// </summary>
        /// <param name="user"></param>
        /// <param name="createNew"></param>
        /// <returns>User id as int</returns>
        /// <exception cref="AppsUsernameAlreadyExistException"></exception>
        /// <exception cref="AppsEmailAlreadyExistException"></exception>
        /// <exception cref="AppsDatabaseExecutionException"></exception>
        /// <exception cref="AppsOutOfAcceptedAgeException"></exception>
        /// <exception cref="AppsRequiredDataIsNullException"></exception>
        /// <exception cref="AppsInvalidDataFormatException"></exception>
        /// <exception cref="AppsDataNotFoundException"></exception>
        /// <exception cref="AppsInvalidEmailFormatException"></exception>
        public async Task<int> SaveUser(User user, bool createNew = false, bool isSupperAdmin = false)
        {
            if (!isSupperAdmin && user.role_id != UserRoleId) throw new AppsUnAuthorizedException();
            if (createNew)
            {
                ValidateUserNameAgainstRequirement(user.username);

                if (!string.IsNullOrWhiteSpace(user.email) && !ValidateEmailFormat(user.email))
                    throw new AppsInvalidEmailFormatException();

                user.password = ValidateAndGeneratePasswordHash(user.password);

                if ((user.dob > DateTime.MinValue) && !ValidateDOBAgaintsAcceptedRange(user.dob))
                    throw new AppsOutOfAcceptedAgeException();

                user.gender = string.IsNullOrWhiteSpace(user.gender) ? GenderEnum.Male.ToString() : user.gender;

                var newUser = new user();
                ModelObjectHelper.CopyObject(user, newUser);
                var id = await Context.CreateUser(newUser);
                return id;
            }
            else
            {
                var dbUser = Context.GetUser(i => i.id == user.id);

                if (dbUser == null) throw new AppsDataNotFoundException("This user is no longer exist");

                if (!string.IsNullOrWhiteSpace(user.password))
                    dbUser.password = ValidateAndGeneratePasswordHash(user.password);

                if (user.dob > DateTime.MinValue && !ValidateDOBAgaintsAcceptedRange(user.dob)) throw new AppsOutOfAcceptedAgeException();

                if (!string.IsNullOrWhiteSpace(user.email) && !ValidateEmailFormat(user.email)) throw new AppsInvalidEmailFormatException();

                dbUser.email = user.email;
                dbUser.dob = user.dob;
                dbUser.image = dbUser.image != user.image && !string.IsNullOrWhiteSpace(user.image) ? user.image : dbUser.image;
                dbUser.display_name = user.display_name;
                dbUser.gender = string.IsNullOrWhiteSpace(user.gender) ? GenderEnum.Male.ToString() : user.gender;

                await Context.UpdateUser(dbUser);
                return user.id;                
            }
        }
 public async Task UpdateUser(user updatedUser)
 {
     if (!GetRoles().Any(i => i.id == updatedUser.role_id)) throw new AppsRequiredDataIsNullException();
     using (var context = new appsterEntities())
     {
         var checkData = context.users.SingleOrDefault(i => i.id != updatedUser.id &&
                         (i.username == updatedUser.username || i.email == updatedUser.email));
         if (checkData == null)
         {
             context.users.Add(updatedUser);
             context.Entry(updatedUser).State = System.Data.Entity.EntityState.Modified;
             await context.SaveChangesAsync();
         }
         else
         {
             if (checkData.username == updatedUser.username) throw new AppsUsernameAlreadyExistException();
             if (checkData.email == updatedUser.email) throw new AppsEmailAlreadyExistException();
         }
     }
 }
 public async Task<int> CreateUser(user newUser)
 {
     using (var context = new appsterEntities())
     {
         var checkData = context.users.FirstOrDefault(i => i.username == newUser.username || i.email == newUser.email);
         if (checkData == null)
         {
             context.users.Add(newUser);
             await context.SaveChangesAsync();
             return newUser.id;
         }
         else
         {
             if (checkData.username == newUser.username) throw new AppsUsernameAlreadyExistException();
             if (checkData.email == newUser.email) throw new AppsEmailAlreadyExistException();
         }
         throw new AppsDatabaseExecutionException();
     }
 }