Sha256: 56a397632979c2f25343e7a62da4e8f8b6d80994f8cd701c124357cadc1bf567

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

module Notee
  class User < ActiveRecord::Base
    # enums
    enum role: { writer: 0, editor: 10, manager: 20, suspended: 99 }

    # writer
    # - create: 	posts, categories, images
    # - update: 	my posts, my user
    # - delete: 	my posts (Logical delete)

    # editor
    # - create: 	posts, categories, images
    # - update: 	posts, categories, images, my user
    # - delete: 	posts, categories, images (Logical delete)

    # manager
    # - create: 	posts, categories, images, users
    # - update: 	posts, categories, images, users
    # - delete: 	posts, categories, images, users (Logical delete)

    # suspended
    # all none

    # root
    # all

    # accessors
    attr_accessor :file
    attr_accessor :password
    attr_accessor :password_confirm
    attr_accessor :editor_id

    # callback
    before_save :confirm_password
    before_save :encrypt_password
    before_save :manage_profile_img

    def sign_in(name_or_email, password)
      user = find_by(name: name_or_email)
      user = find_by(email: name_or_email) unless user
      return false unless user
      return false unless user.encrypted_password == encrypt(password)

      user
    end

    def encrypt(password)
      OpenSSL::Digest::MD5.hexdigest(password)
    end

    def confirm_password
      return false unless password == password_confirm
    end

    def encrypt_password
      self.encrypted_password = encrypt(password)
    end

    def manage_profile_img
      return unless file
      return if User.exists?(profile_img: file)

      image_dir = Rails.root.to_s + '/public/notee/profile/'
      FileUtils.mkdir_p(image_dir) unless FileTest.exist?(image_dir)
      image_name = Time.now.strftime('%Y%m%d%H%M%S') + '--' + SecureRandom.uuid + '.jpg'
      transaction do
        open(image_dir + '/' + image_name, 'wb') do |output|
          output.write(file.read)
        end
      end
      self.profile_img = image_name
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
notee-0.4.0 app/models/notee/user.rb