Completed
Push — master ( 279e03...b6f333 )
by Auke
15s
created

mod_auth_radius::authExternalUser()   C

Complexity

Conditions 17
Paths 20

Size

Total Lines 79
Code Lines 52

Duplication

Lines 38
Ratio 48.1 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 52
nc 20
nop 2
dl 38
loc 79
ccs 0
cts 66
cp 0
crap 306
rs 5.083
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
	include_once($ariadne."/modules/mod_auth/default.php");
4
5
	class mod_auth_radius extends mod_auth_default {
6
7
		function __construct($config) {
8
			$this->config = $config;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
9
		}
10
11 View Code Duplication
		function storeExternalUser($login, $userData) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
		global $AR, $store;
13
			// become admin for the moment
14
			$aLogin		= $this->config["import_user_by"];
15
			$AR->user	= current($store->call("system.get.phtml", "", $store->find("/system/users/", "login.value='$aLogin' and object.implements='puser'")));
16
17
			$user = $this->getUser($login);
18
			if ($user !== true) {
19
				$user_dir		= $this->config["import_user_directory"];
20
				$user_profile		= $this->config["import_user_profile"];
21
				$user_type		= $this->config["import_user_type"];
22
				if (!$user_type) {
23
					$user_type = "puser";
24
				}
25
26
				debug("ldAuthRadius: user ($login) didn't exist before: creating", "all");
27
				$data = $userData;
28
				$data["arNewFilename"] = "$user_dir$login/";
29
				$data["profile"] = $user_profile;
30
				$data["setowner"] = true;
31
32
				$user = $store->newobject(
33
							"$user_dir$login/",
34
							"$user_dir",
35
							"$user_type",
36
							new object);
37
38
				$user->arIsNewObject = true;
39
				$user->call('system.save.data.phtml', $data);
40
41
				return $user;
42
			} else {
43
				// $AR->user was set by getUser and contains the correct user now.
44
				return $AR->user;
45
			}
46
	 	}
47
48
	 	function authExternalUser($login, $password) {
0 ignored issues
show
Coding Style introduced by
authExternalUser uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
49
50
			$res = radius_auth_open();
51
			if (!radius_add_server($res, $this->config['radius_server'], $this->config['radius_port'], $this->config['sharedsecret'], 3, 3)) {
52
				debug('RadiusError:' . radius_strerror($res). "\n",'auth');
53
				return false;
54
			}
55
56
			if (!radius_create_request($res, RADIUS_ACCESS_REQUEST)) {
57
				debug('RadiusError:' . radius_strerror($res). "\n",'auth');
58
				return false;
59
			}
60
61 View Code Duplication
			if (!radius_put_string($res, RADIUS_NAS_IDENTIFIER, isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost'))  {
62
				debug('RadiusError:' . radius_strerror($res). "\n",'auth');
63
				return false;
64
			}
65
66 View Code Duplication
			if (!radius_put_int($res, RADIUS_SERVICE_TYPE, RADIUS_FRAMED)) {
67
				debug('RadiusError:' . radius_strerror($res). "\n",'auth');
68
				return false;
69
			}
70
71 View Code Duplication
			if (!radius_put_int($res, RADIUS_FRAMED_PROTOCOL, RADIUS_PPP)) {
72
				debug('RadiusError:' . radius_strerror($res). "\n",'auth');
73
				return false;
74
			}
75
76 View Code Duplication
			if (!radius_put_string($res, RADIUS_CALLING_STATION_ID, isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : '127.0.0.1') == -1) {
77
				debug('RadiusError:' . radius_strerror($res). "\n",'auth');
78
				return false;
79
			}
80
81 View Code Duplication
			if (!radius_put_string($res, RADIUS_USER_NAME, $login)) {
82
				debug('RadiusError:' . radius_strerror($res). "\n",'auth');
83
				return false;
84
			}
85 View Code Duplication
			if($password){
86
				if (!radius_put_string($res, RADIUS_USER_PASSWORD, $password)) {
87
					debug('RadiusError:' . radius_strerror($res). "\n",'auth');
88
					return false;
89
				}
90
			}
91
92 View Code Duplication
			if (!radius_put_int($res, RADIUS_SERVICE_TYPE, RADIUS_FRAMED)) {
93
				debug('RadiusError:' . radius_strerror($res). "\n",'auth');
94
				return false;
95
			}
96
97 View Code Duplication
			if (!radius_put_int($res, RADIUS_FRAMED_PROTOCOL, RADIUS_PPP)) {
98
				debug('RadiusError:' . radius_strerror($res). "\n",'auth');
99
				return false;
100
			}
101
102
			$req = radius_send_request($res);
103 View Code Duplication
			if (!$req) {
104
				debug('RadiusError:' . radius_strerror($res). "\n",'auth');
105
				return false;
106
			}
107
108
			$user = false;
109
			switch($req) {
110
				case RADIUS_ACCESS_ACCEPT:
111
					$userData = Array();
112
					$userData["name"] = $login;
113
					$userData["newpass1"] = '!';
114
					$userData["newpass2"] = '!';
115
	 				$user = $this->storeExternalUser($login, $userData);
116
					break;
117
118
				case RADIUS_ACCESS_REJECT:
119
					debug("RadiusError: Radius Request rejected\n",'auth');
120
					break;
121
122
				default:
123
					debug("RadiusError: Unknown answer\n",'auth');
124
			}
125
			return $user;
126
	 	}
127
	}
128