Zend framework 2: Invalid instantiator of type "NULL" for "Zend\Form\FormInterface" - zend-form

I am not able to instantiate my forms via DI due to strange exception.
Here is the code snippet to reproduce exception I got
include 'Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true
)
));
include 'Zend/Di/Di.php';
$di = new Zend\Di\Di();
$form = $di->get('Zend\Form\Form');
I always get
Fatal error: Uncaught exception 'Zend\Di\Exception\RuntimeException' with message 'Invalid instantiator of type "NULL" for "Zend\Form\FormInterface".'
What am I doing wrong? Any help and/or suggestions are greatly appreciated.

I digged into this and indeed an exception is thrown. Try running following:
$def = new \Zend\Di\Definition\RuntimeDefinition();
var_dump($def->getMethods('Zend\Form\Form'));
This will dump all methods that Zend\Di will understand as "injection methods". The output will be something like
array (size=23)
'__construct' => boolean true
'setData' => boolean false
'setHydrator' => boolean false
'setBindOnValidate' => boolean false
'setBaseFieldset' => boolean false
'setValidationGroup' => boolean false
'setInputFilter' => boolean false
'setUseInputFilterDefaults' => boolean false
'setPreferFormInputFilter' => boolean false
'setWrapElements' => boolean false
'setOptions' => boolean false
'setFormFactory' => boolean true
'setPriority' => boolean false
'setMessages' => boolean false
'setObject' => boolean false
'setUseAsBaseFieldset' => boolean false
'setName' => boolean false
'setAttribute' => boolean false
'setAttributes' => boolean false
'setValue' => boolean false
'setLabel' => boolean false
'setLabelAttributes' => boolean false
'prepareElement' => boolean true
As you can see, method prepareElement(FormInterface $form) is marked as injection method. That is because (as of current Di introspection strategy) prepareElement is defined in interface Zend\Form\ElementPrepareAwareInterface, and that since 2.1.0.
Since injection methods are marked to be called by Zend\Di after the object is instantiated, Zend\Di is hereby failing to find an instance for Zend\Form\FormInterface (which isn't Zend\Form\Form).
So as you can see the change has nothing to do with Zend\Di in this case. You could eventually report it or alter the introspection strategy to suit your needs.
I personally don't think Zend\Di should call that method, nor that the method should be part of an *Aware* interface.

Related

Why not infer T::Boolean for true and false?

Sorbet infers the type of true to be TrueClass, and the type of false to be FalseClass. Often it would be nice if it would instead infer T::Boolean. Why not special case true and false to have the type T::Boolean instead?
It's possible to work around this problem with a type annotation, initializing variables with T.let(true, T::Boolean) for example, but it would be nice to not have to provide this extra information.
# typed: true
T.reveal_type(true) # Revealed type: `TrueClass`
T.reveal_type(false) # Revealed type: `FalseClass`
extend T::Sig
sig {params(x: T::Boolean).void}
def test(x)
var = true
10.times do
var = false # Changing the type of a variable in a loop is not permitted
end
end
The assignment of false to var in the loop causes an error to be raised, as the type of var is being changed from TrueClass to FalseClass.
Sorbet's flow-sensitive typing is made more precise by true and false having different types. In the following example, a variable with the value true is used as the condition of an if-statement:
# typed: true
val = true
if val
puts "true!"
else
puts "false?"
end
The resulting error from sorbet is:
editor.rb:7: This code is unreachable https://srb.help/7006
7 | puts "false?"
^^^^^^^^
Errors: 1
Behind the scenes, sorbet knows that the value being examined has the type TrueClass, and that the value true is the only value of that type. As a result, it knows that val cannot be false, and that the else branch will never be executed.
Now consider the case where we instead infer the type T::Boolean for true and false. T::Boolean is a synonym for T.any(TrueClass, FalseClass), so in the example it now means that val could be either true or false. As a result, it becomes impossible to tell from the type alone that the else branch will not be executed.
The flow-sensitive typing documentation on sorbet.org has more information on this topic.
Asked the same question today as well.
Ended up fixing it as you suggested with type annotations and initializing with T.let(true, T::Boolean)
# typed: true
extend T::Sig
sig {params(x: T::Boolean).void}
def test(x)
var = T.let(true, T::Boolean)
10.times do
var = T.let(false, T::Boolean)
end
end

Impersonation on IIS 7.5 - fileaccess on network drive - System.UnauthorizedAccessException

Environment: IIS 7.5, Windows 7 Pro, Visual Studio 2012, Webapp uses Framework 4.0
I tried to get a directory listing from a fileserver using path like \\server\share\directory by a webservice (asmx).
I use the following configuration in web.config:
<identity impersonate="true" />
<authentication mode="Windows" />
I check User.Identity.Name and Threading.Thread.CurrentPrincipal.Identity.Name and get the used Domain\Username for login. A "System.UnauthorizedAccessException" is thrown if i try to get a directory listing of "\\server\share\directory" .
I tried many things, even to use local system, local service or the domain administrator as identity for the application-pool.
When I change the configuration system.webServer/serverRuntime authenticatedUserOverride from UseAuthenticatedUser to UseWorkerProcessUser, i can reach the network share, but under the identity of the application-pool user and not of the user, that uses the webservice. In this configuration i cannot get Information about the login, that was used, instead i get as identity always the user for the application-pool.
Is possible to get the windows-login-user without impersonate?
Or what have to be done to get successfull access to networkfilesystem?
I think UAC is dissabled on my machine.
After trying a lot of things i found this solution (not perfect, but it works for me):
ADS/Domaincontroller:
Add a new user "IIS-User" to active directory
Add Group Domain Admins to "IIS-User"
set Domain Admins to primary Group
Edit/add attribute servicePrincipalName by the attribute-editor : http/[computername of iis]
save and close usereditor in ADS-Server-Manager
open usereditor -> new tab "delegation" -> Allowing user "IIS-User" the delegation of services
PC/Server with IIS:
open IIS-Manger
edit application-pool: set identity to new user for the application-pool, that is used for the webservice/asp-app
edit configuration with configuration editor of IIS-manger:
system.web/authentication mode=Windows
system.web/identity impersonate=True
system.webServer/security/authentication/windowsAuthentication enabled=True, useAppPoolCredebtials=True
system.webServer/serverRuntime authenticatedUserOverride=UseAuthenticatedUser
system.webServer/validation validateIntegratedModeConfiguration=false
Now the webservice can access the netshare with the rights of the new user ("IIS-User"), but the property User.Identity.Name returns the name of the login user. But now i can check, wether the login user has access to files/directory and show only for the user accessible files/directories.
Maybe there are some unnecessary configuration settings, but after searching many hours for a solution i'm glad to found a working solution.
Private Shared Function getSecId4User(user As IPrincipal) As SecurityIdentifier
Return getSecId4Username(user.Identity.Name)
End Function
Private Shared Function getSecId4Username(username As String) As SecurityIdentifier
Dim account As New NTAccount(username)
Return account.Translate(GetType(SecurityIdentifier))
End Function
Private Shared Function isVisible4User(di As DirectoryInfo, secId As SecurityIdentifier) As Boolean
Dim dirSec As DirectorySecurity = di.GetAccessControl
Dim authRules As AuthorizationRuleCollection = dirSec.GetAccessRules(True, True, GetType(SecurityIdentifier))
For Each ar As FileSystemAccessRule In authRules
If secId.Equals(CType(ar.IdentityReference, SecurityIdentifier)) Then
Dim fileSystemRights As FileSystemRights = ar.FileSystemRights
Select Case fileSystemRights
Case System.Security.AccessControl.FileSystemRights.FullControl
Return True
Case System.Security.AccessControl.FileSystemRights.AppendData
Return True
Case System.Security.AccessControl.FileSystemRights.ChangePermissions
Return True
Case System.Security.AccessControl.FileSystemRights.CreateDirectories
Return True
Case System.Security.AccessControl.FileSystemRights.CreateFiles
Return True
Case System.Security.AccessControl.FileSystemRights.Delete
Return True
Case System.Security.AccessControl.FileSystemRights.DeleteSubdirectoriesAndFiles
Return True
Case System.Security.AccessControl.FileSystemRights.ExecuteFile
Return True
Case System.Security.AccessControl.FileSystemRights.FullControl
Return True
Case System.Security.AccessControl.FileSystemRights.ListDirectory
Return True
Case System.Security.AccessControl.FileSystemRights.Modify
Return True
Case System.Security.AccessControl.FileSystemRights.Read
Return True
Case System.Security.AccessControl.FileSystemRights.ReadAndExecute
Return True
Case System.Security.AccessControl.FileSystemRights.ReadAttributes
Return True
Case System.Security.AccessControl.FileSystemRights.ReadData
Return True
Case System.Security.AccessControl.FileSystemRights.ReadExtendedAttributes
Return True
Case System.Security.AccessControl.FileSystemRights.ReadPermissions
Return True
Case System.Security.AccessControl.FileSystemRights.Synchronize
Return True
Case System.Security.AccessControl.FileSystemRights.TakeOwnership
Return True
Case System.Security.AccessControl.FileSystemRights.Traverse
Return True
Case System.Security.AccessControl.FileSystemRights.Write
Return True
Case System.Security.AccessControl.FileSystemRights.WriteAttributes
Return True
Case System.Security.AccessControl.FileSystemRights.WriteData
Return True
Case System.Security.AccessControl.FileSystemRights.WriteExtendedAttributes
Return True
Case Else
End Select
End If
Next
Return False
End Function
Private Shared Function isVisible4User(fi As FileInfo, secId As SecurityIdentifier) As Boolean
Dim filesec As FileSecurity = fi.GetAccessControl
Dim authRules As AuthorizationRuleCollection = filesec.GetAccessRules(True, True, GetType(SecurityIdentifier))
For Each ar As FileSystemAccessRule In authRules
If secId.CompareTo(CType(ar.IdentityReference, SecurityIdentifier)) = 0 Then
Dim fileSystemRights As FileSystemRights = ar.FileSystemRights
Select Case fileSystemRights
Case System.Security.AccessControl.FileSystemRights.FullControl
Return True
Case System.Security.AccessControl.FileSystemRights.AppendData
Return True
Case System.Security.AccessControl.FileSystemRights.ChangePermissions
Return True
Case System.Security.AccessControl.FileSystemRights.CreateDirectories
Return True
Case System.Security.AccessControl.FileSystemRights.CreateFiles
Return True
Case System.Security.AccessControl.FileSystemRights.Delete
Return True
Case System.Security.AccessControl.FileSystemRights.DeleteSubdirectoriesAndFiles
Return True
Case System.Security.AccessControl.FileSystemRights.ExecuteFile
Return True
Case System.Security.AccessControl.FileSystemRights.FullControl
Return True
Case System.Security.AccessControl.FileSystemRights.ListDirectory
Return True
Case System.Security.AccessControl.FileSystemRights.Modify
Return True
Case System.Security.AccessControl.FileSystemRights.Read
Return True
Case System.Security.AccessControl.FileSystemRights.ReadAndExecute
Return True
Case System.Security.AccessControl.FileSystemRights.ReadAttributes
Return True
Case System.Security.AccessControl.FileSystemRights.ReadData
Return True
Case System.Security.AccessControl.FileSystemRights.ReadExtendedAttributes
Return True
Case System.Security.AccessControl.FileSystemRights.ReadPermissions
Return True
Case System.Security.AccessControl.FileSystemRights.Synchronize
Return True
Case System.Security.AccessControl.FileSystemRights.TakeOwnership
Return True
Case System.Security.AccessControl.FileSystemRights.Traverse
Return True
Case System.Security.AccessControl.FileSystemRights.Write
Return True
Case System.Security.AccessControl.FileSystemRights.WriteAttributes
Return True
Case System.Security.AccessControl.FileSystemRights.WriteData
Return True
Case System.Security.AccessControl.FileSystemRights.WriteExtendedAttributes
Return True
Case Else
End Select
End If
Next
Return False
End Function
Checking of accessibility

Puppet - hiera not assigning parameters to module

According to the Puppet documentation on hiera, the following example should work, but for some reason it's not passing the parameters to the module.
I have a simple module called hello, which takes in parameters with hello::talk, and outputs a file at /home/user/hello.txt.
Here's the class:
class hello {
// other stuff
}
define hello::talk (
$say_hello = true,
$message = '',
$say_goodbye = false,
) {
file { '/home/user/hello.txt':
owner => root, group => root, mode => 0755,
ensure => file,
content => template('hello/template.erb')
}
}
If within a node, I use it like so:
node foo {
include hello
hello::talk {'config':
say_hello = true,
message = 'howdy!',
say_goodbye = false,
}
}
It works as expected (Creating the file etc)
But if I try to do it with hiera, I can only get it to include the class, but not do the hello::talk bit.
Here's my common.yaml:
classes:
- hello
hello::talk -
say_hello = true
message = "Oh dear ..."
say_goodbye = false
And site.pp:
hiera_include('classes')
node foo {
}
No file is made :(
UPDATE
Okay, so I misunderstood the documentation a little Lol ... And I'm still misunderstanding the difference between class and define ... But I guess my brain will get to that later
I managed to get it to work by changing define hello::talk to class hello::talk and the common.yaml file to:
classes:
- hello
- hello::talk
hello::talk::say_hello = true
hello::talk::message = 'Yay! ... Kinda'
hello::talk::say_goodbye = true
This leads me onto a new question ... Is there a way that I can restructure the class, so that, like other modules, I only need to include hello and can then set the parameters. Also, it would be nice if I wouldn't have to continually use hello::talk:: to set the parameters ...
The main difference between classes and defines is that a class can be called only once and defines can be called multiple times if you provide different names.
Regarding your last question.
You will need to move your parameters inside your hello class and call the hello::talk class/define inside this (I will presume that hello::talk is a define here):
class hello (
say_hello = true,
message = 'howdy!',
say_goodbye = false
) {
hello::talk {'config':
say_hello => $say_hello,
message => $message ,
say_goodbye => $say_goodbye,
}
}
With the above structure you can set in hiera:
classes:
- hello
hello::say_hello: true
hello::message: 'Yay! ... Kinda'
hello::say_goodbye: true

undefined method id for TrueClass Rails

def can_save(board,role)
if ar_user = already_registered?(email) || user = self.save
Participant.make(ar_user||user, board, role)
end
user
end
I do this rspec test on it:
it "should return the user if the user is not already registered" do
lambda do
user = #new_user.can_save(#board, "Manager")
end.should change(User,:count).by(1)
end
I get this error:
undefined method `id' for true:TrueClass
Why is that?
save method returns true or false, not a saved instance of object.

Testing for false with the Twitter Gem in rails 3

I'm using the Twitter gem, and I want to take a list of users from my Friend model and test to see if they're being following a Twitter user, then ... unfollow if the Twitter API returns false. Trouble is, it seems the code I'm using below never evaluates as false, even when the API returns false so it's always reverting to the else section.
I've tried variations like if ret == 'false' and it's not worked.
ret = #t.friendship?(#id_str, f.screen_name)
if ret == false
#t.unfollow(f.screen_name)
puts "#{f.screen_name} has been unfollowed."
self.update_unfollowed_column(f.id, false)
else
self.update_following_column(f.id)
end
In the console, though, if I do:
r = t.friendship?('user1', 'user2')
=> false
And then, I can do:
r == false
=> true
So, I'm not sure exactly what I'm doing wrong with the code in my Friend model.
Before the if statement, try debugging what the result is from the friendship? method:
logger.info ret.class.name # => FalseClass if it's actually false,
# => String if it's 'false'
logger.info ret.inspect # => "false" if it's false.
logger.info((ret == false).inspect) # => Should be "true"