Module: MediaWiki::Auth

Included in:
Butt
Defined in:
lib/mediawiki/auth.rb

Instance Method Summary collapse

Instance Method Details

#create_account(username, password, language = 'en', reason = nil) ⇒ Boolean

Creates an account using the standard procedure. Use the language code, not the name.

Parameters:

  • username (String)

    The desired username.

  • password (String)

    The desired password.

  • language (String) (defaults to: 'en')

    The language code to be set as default for the account. Defaults to ‘en’, or English.

  • reason (String) (defaults to: nil)

    The reason for creating the account, as shown in the account creation log. Optional.

Returns:

  • (Boolean)

    True if successful, false if not.

See Also:

Since:

  • 0.1.0



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mediawiki/auth.rb', line 68

def (username, password, language = 'en', reason = nil)
  params = {
    name: username,
    password: password,
    language: language,
    token: get_token('createaccount')
  }
  params[:reason] = reason unless reason.nil?

  result = post(params)
  unless result['error'].nil?
    raise MediaWiki::Butt::AuthenticationError.new(result['error']['code'])
  end

  result['createaccount']['result'] == 'Success'
end

#create_account_email(username, email, language = 'en', reason = nil) ⇒ Boolean

Creates an account using the random password sent by email procedure.

Parameters:

  • email (String)

    The desired email address

  • username (String)

    The desired username.

  • password (String)

    The desired password.

  • language (String) (defaults to: 'en')

    The language code to be set as default for the account. Defaults to ‘en’, or English.

  • reason (String) (defaults to: nil)

    The reason for creating the account, as shown in the account creation log. Optional.

Returns:

  • (Boolean)

    True if successful, false if not.

See Also:

Since:

  • 0.1.0



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mediawiki/auth.rb', line 92

def (username, email, language = 'en', reason = nil)
  params = {
    name: username,
    email: email,
    mailpassword: 'value',
    language: language,
    token: get_token('createaccount')
  }
  params[:reason] = reason unless reason.nil?

  result = post(params)
  unless result['error'].nil?
    raise MediaWiki::Butt::AuthenticationError.new(result['error']['code'])
  end

  result['createaccount']['result'] == 'Success'
end

#login(username, password) ⇒ Boolean

Logs the user into the wiki. This is generally required for editing and getting restricted data.

Parameters:

  • username (String)

    The username

  • password (String)

    The password

Returns:

  • (Boolean)

    True if the login was successful.

Raises:

  • (AuthenticationError)

See Also:

Since:

  • 0.1.0



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mediawiki/auth.rb', line 12

def (username, password)
  # Save the assertion value while trying to log in, because otherwise the assertion will prevent us from logging in
  assertion_value = @assertion.clone
  @assertion = nil

  params = {
    action: 'login',
    lgname: username,
    lgpassword: password,
    lgtoken: get_token('login')
  }

  response = post(params)

  @assertion = assertion_value

  result = response['login']['result']

  if result == 'Success'
    @name = response['login']['lgusername']
    @logged_in = true
    return true
  end

  raise MediaWiki::Butt::AuthenticationError.new(result)
end

#logoutBoolean

Logs the current user out.

Returns:

  • (Boolean)

    True if it was able to log anyone out, false if not.

See Also:

Since:

  • 0.1.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mediawiki/auth.rb', line 43

def logout
  if @logged_in
    params = {
      action: 'logout'
    }

    post(params)
    @logged_in = false

    true
  else
    false
  end
end