<commit_before>jraphical = require 'jraphical'

Flaggable = require '../../traits/flaggable'

module.exports = class JUser extends jraphical.Module
  {secure}       = require 'bongo'
  {daisy, dash}  = require 'sinkrow'

  JAccount       = require '../account'
  JSession       = require '../session'
  JInvitation    = require '../invitation'
  JName          = require '../name'
  JGroup         = require '../group'
  JLog           = require '../log'
  JMail          = require '../email'

  createId       = require 'hat'

  {Relationship} = jraphical

  createKodingError =(err)->
    if 'string' is typeof err
      message: err
    else
      message: err.message

  @bannedUserList = ['abrt','amykhailov','apache','about','visa','shared-',
                     'cthorn','daemon','dbus','dyasar','ec2-user','http',
                     'games','ggoksel','gopher','haldaemon','halt','mail',
                     'nfsnobody','nginx','nobody','node','operator','https',
                     'root','rpcuser','saslauth','shutdown','sinanlocal',
                     'sshd','sync','tcpdump','uucp','vcsa','zabbix',
                     'search','blog','activity','guest','credits','about',
                     'kodingen','alias','backup','bin','bind','daemon',
                     'Debian-exim','dhcp','drweb','games','gnats','klog',
                     'kluser','libuuid','list','mhandlers-user','more',
                     'mysql','nagios','news','nobody','popuser','postgres',
                     'proxy','psaadm','psaftp','qmaild','qmaill','qmailp',
                     'qmailq','qmailr','qmails','sshd','statd','sw-cp-server',
                     'sync','syslog','tomcat','tomcat55','uucp','what',
                     'www-data','fuck','porn','p0rn','porno','fucking',
                     'fucker','admin','postfix','puppet','main','invite',
                     'administrator','members','register','activate','shared',
                     'groups','blogs','forums','topics','develop','terminal',
                     'term','twitter','facebook','google','framework', 'kite']

  @hashUnhashedPasswords =->
    @all {salt: $exists: no}, (err, users)->
      users.forEach (user)-> user.changePassword user.getAt('password')

  hashPassword =(value, salt)->
    require('crypto').createHash('sha1').update(salt+value).digest('hex')

  createSalt = require 'hat'

  @share()

  @trait __dirname, '../../traits/flaggable'

  @getFlagRole =-> 'owner'

  @set
    softDelete      : yes
    broadcastable   : no
    indexes         :
      username      : 'unique'
      email         : 'unique'
      'foreignAuth.github.foreignId' : 1

    sharedEvents    :
      static        : [
        { name : "RemovedFromCollection" }
      ]
      instance      : [
        { name : "RemovedFromCollection" }
      ]
    sharedMethods   :
      instance      : ['sendEmailConfirmation']
      static        : [
        'login','logout','register','usernameAvailable','emailAvailable',
        'changePassword','changeEmail','fetchUser','setDefaultHash','whoami',
        'isRegistrationEnabled','convert','setSSHKeys', 'getSSHKeys',
        'authenticateWithOauth','unregister'
      ]

    schema          :
      username      :
        type        : String
        validate    : require('../name').validateName
        set         : (value)-> value.toLowerCase()
      oldUsername   : String
      uid           :
        type        : Number
        set         : Math.floor
      email         :
        type        : String
        email       : yes
      password      : <PASSWORD>
      salt          : String
      blockedUntil  : Date
      status        :
        type        : String
        enum        : [
          'invalid status type', [
            'unconfirmed','confirmed','blocked','deleted'
          ]
        ]
        default     : 'unconfirmed'
      registeredAt  :
        type        : Date
        default     : -> new Date
      lastLoginDate :
        type        : Date
        default     : -> new Date
      emailFrequency: Object
      onlineStatus  :
        actual      :
          type      : String
          enum      : ['invalid status',['online','offline']]
          default   : 'online'
        userPreference:
          type      : String
          # enum      : ['invalid status',['online','offline','away','busy']]

      sshKeys       : [Object]
      foreignAuth   :
        github      : Object
    relationships       :
      ownAccount        :
        targetType      : JAccount
        as              : 'owner'
      leasedAccount     :
        targetType      : JAccount
        as              : 'leasor'
      emailConfirmation :
        targetType      : require '../emailconfirmation'
        as              : 'confirmation'

  sessions  = {}
  users     = {}
  guests    = {}

  @unregister = secure (client, confirmUsername, callback) ->
    {delegate} = client.connection
    if delegate.type is 'unregistered'
      return callback createKodingError "You are not registered!"
    unless confirmUsername is delegate.profile.nickname<commit_msg>unregister: let admins also unregister users<commit_after>jraphical = require 'jraphical'

Flaggable = require '../../traits/flaggable'

module.exports = class JUser extends jraphical.Module
  {secure}       = require 'bongo'
  {daisy, dash}  = require 'sinkrow'

  JAccount       = require '../account'
  JSession       = require '../session'
  JInvitation    = require '../invitation'
  JName          = require '../name'
  JGroup         = require '../group'
  JLog           = require '../log'
  JMail          = require '../email'

  createId       = require 'hat'

  {Relationship} = jraphical

  createKodingError =(err)->
    if 'string' is typeof err
      message: err
    else
      message: err.message

  @bannedUserList = ['abrt','amykhailov','apache','about','visa','shared-',
                     'cthorn','daemon','dbus','dyasar','ec2-user','http',
                     'games','ggoksel','gopher','haldaemon','halt','mail',
                     'nfsnobody','nginx','nobody','node','operator','https',
                     'root','rpcuser','saslauth','shutdown','sinanlocal',
                     'sshd','sync','tcpdump','uucp','vcsa','zabbix',
                     'search','blog','activity','guest','credits','about',
                     'kodingen','alias','backup','bin','bind','daemon',
                     'Debian-exim','dhcp','drweb','games','gnats','klog',
                     'kluser','libuuid','list','mhandlers-user','more',
                     'mysql','nagios','news','nobody','popuser','postgres',
                     'proxy','psaadm','psaftp','qmaild','qmaill','qmailp',
                     'qmailq','qmailr','qmails','sshd','statd','sw-cp-server',
                     'sync','syslog','tomcat','tomcat55','uucp','what',
                     'www-data','fuck','porn','p0rn','porno','fucking',
                     'fucker','admin','postfix','puppet','main','invite',
                     'administrator','members','register','activate','shared',
                     'groups','blogs','forums','topics','develop','terminal',
                     'term','twitter','facebook','google','framework', 'kite']

  @hashUnhashedPasswords =->
    @all {salt: $exists: no}, (err, users)->
      users.forEach (user)-> user.changePassword user.getAt('password')

  hashPassword =(value, salt)->
    require('crypto').createHash('sha1').update(salt+value).digest('hex')

  createSalt = require 'hat'

  @share()

  @trait __dirname, '../../traits/flaggable'

  @getFlagRole =-> 'owner'

  @set
    softDelete      : yes
    broadcastable   : no
    indexes         :
      username      : 'unique'
      email         : 'unique'
      'foreignAuth.github.foreignId' : 1

    sharedEvents    :
      static        : [
        { name : "RemovedFromCollection" }
      ]
      instance      : [
        { name : "RemovedFromCollection" }
      ]
    sharedMethods   :
      instance      : ['sendEmailConfirmation']
      static        : [
        'login','logout','register','usernameAvailable','emailAvailable',
        'changePassword','changeEmail','fetchUser','setDefaultHash','whoami',
        'isRegistrationEnabled','convert','setSSHKeys', 'getSSHKeys',
        'authenticateWithOauth','unregister'
      ]

    schema          :
      username      :
        type        : String
        validate    : require('../name').validateName
        set         : (value)-> value.toLowerCase()
      oldUsername   : String
      uid           :
        type        : Number
        set         : Math.floor
      email         :
        type        : String
        email       : yes
      password      : <PASSWORD>
      salt          : String
      blockedUntil  : Date
      status        :
        type        : String
        enum        : [
          'invalid status type', [
            'unconfirmed','confirmed','blocked','deleted'
          ]
        ]
        default     : 'unconfirmed'
      registeredAt  :
        type        : Date
        default     : -> new Date
      lastLoginDate :
        type        : Date
        default     : -> new Date
      emailFrequency: Object
      onlineStatus  :
        actual      :
          type      : String
          enum      : ['invalid status',['online','offline']]
          default   : 'online'
        userPreference:
          type      : String
          # enum      : ['invalid status',['online','offline','away','busy']]

      sshKeys       : [Object]
      foreignAuth   :
        github      : Object
    relationships       :
      ownAccount        :
        targetType      : JAccount
        as              : 'owner'
      leasedAccount     :
        targetType      : JAccount
        as              : 'leasor'
      emailConfirmation :
        targetType      : require '../emailconfirmation'
        as              : 'confirmation'

  sessions  = {}
  users     = {}
  guests    = {}

  @unregister = secure (client, confirmUsername, callback) ->
    {delegate} = client.connection
    if delegate.type is 'unregistered'
      return callback createKodingError "You are not registered!"
    unless confirmUsername is delegate.profile.nickname or
           delegate.can 'administer accounts'
{'fraction_non_alphanumeric': 0.14726426076833526, 'fraction_numerical': 0.0011641443538998836, 'mean_word_length': 1.8259320175438596, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4886525', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2774, 'n_words': 492}